Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- class NewYearResolution
- {
- static int numberOfLoops;
- static int testCasesCount;
- static string[] targetCalories;
- static int foodCount;
- static bool resultFound;
- static int[] loops;
- static int[,] food;
- static void Main()
- {
- StreamReader input = new StreamReader("input.txt");
- StreamWriter output = new StreamWriter("output.txt");
- testCasesCount = int.Parse(input.ReadLine());
- for (int index = 0; index < testCasesCount; index++)
- {
- resultFound = false;
- targetCalories = input.ReadLine().Split();
- foodCount = int.Parse(input.ReadLine());
- food = new int[foodCount, 3];
- numberOfLoops = foodCount;
- for (int foodIndex = 0; foodIndex < foodCount; foodIndex++)
- {
- string[] foodValues = input.ReadLine().Split();
- food[foodIndex, 0] = int.Parse(foodValues[0]);
- food[foodIndex, 1] = int.Parse(foodValues[1]);
- food[foodIndex, 2] = int.Parse(foodValues[2]);
- }
- while (numberOfLoops > 0)
- {
- loops = new int[numberOfLoops];
- FindSubsets(0, 0);
- numberOfLoops--;
- if (resultFound)
- {
- break;
- }
- }
- if (resultFound)
- {
- output.WriteLine("Case #{0}: yes", index + 1);
- }
- else
- {
- output.WriteLine("Case #{0}: no", index + 1);
- }
- }
- output.Close();
- input.Close();
- }
- static void FindSubsets(int currentLoop, int lastNumber)
- {
- if (currentLoop == numberOfLoops)
- {
- CheckSubset();
- return;
- }
- for (int nextNumber = lastNumber; nextNumber < foodCount; nextNumber++)
- {
- loops[currentLoop] = nextNumber;
- FindSubsets(currentLoop + 1, nextNumber + 1);
- if (resultFound)
- {
- return;
- }
- }
- }
- private static void CheckSubset()
- {
- int totalProtein = 0;
- int totalCarboHydrats = 0;
- int totalFat = 0;
- for (int index = 0; index < loops.Length; index++)
- {
- totalProtein += food[loops[index], 0];
- totalCarboHydrats += food[loops[index], 1];
- totalFat += food[loops[index], 2];
- }
- if (totalProtein == int.Parse(targetCalories[0]) &&
- totalCarboHydrats == int.Parse(targetCalories[1]) &&
- totalFat == int.Parse(targetCalories[2]))
- {
- resultFound = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement