Advertisement
Guest User

New Year Resolution

a guest
Jan 11th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. class NewYearResolution
  4. {
  5.     static int numberOfLoops;
  6.     static int testCasesCount;
  7.     static string[] targetCalories;
  8.     static int foodCount;
  9.     static bool resultFound;
  10.  
  11.     static int[] loops;
  12.     static int[,] food;
  13.  
  14.     static void Main()
  15.     {
  16.         StreamReader input = new StreamReader("input.txt");
  17.         StreamWriter output = new StreamWriter("output.txt");
  18.        
  19.         testCasesCount = int.Parse(input.ReadLine());
  20.  
  21.         for (int index = 0; index < testCasesCount; index++)
  22.         {
  23.             resultFound = false;
  24.             targetCalories = input.ReadLine().Split();
  25.             foodCount = int.Parse(input.ReadLine());
  26.             food = new int[foodCount, 3];
  27.             numberOfLoops = foodCount;
  28.  
  29.             for (int foodIndex = 0; foodIndex < foodCount; foodIndex++)
  30.             {
  31.                 string[] foodValues = input.ReadLine().Split();
  32.                 food[foodIndex, 0] = int.Parse(foodValues[0]);
  33.                 food[foodIndex, 1] = int.Parse(foodValues[1]);
  34.                 food[foodIndex, 2] = int.Parse(foodValues[2]);
  35.             }
  36.  
  37.             while (numberOfLoops > 0)
  38.             {
  39.                 loops = new int[numberOfLoops];
  40.                 FindSubsets(0, 0);
  41.                 numberOfLoops--;
  42.  
  43.                 if (resultFound)
  44.                 {
  45.                     break;
  46.                 }
  47.             }
  48.  
  49.             if (resultFound)
  50.             {
  51.                 output.WriteLine("Case #{0}: yes", index + 1);
  52.             }
  53.             else
  54.             {
  55.                 output.WriteLine("Case #{0}: no", index + 1);
  56.             }
  57.  
  58.         }
  59.  
  60.         output.Close();
  61.         input.Close();
  62.     }
  63.  
  64.     static void FindSubsets(int currentLoop, int lastNumber)
  65.     {
  66.         if (currentLoop == numberOfLoops)
  67.         {
  68.             CheckSubset();
  69.             return;
  70.         }
  71.  
  72.         for (int nextNumber = lastNumber; nextNumber < foodCount; nextNumber++)
  73.         {
  74.             loops[currentLoop] = nextNumber;
  75.             FindSubsets(currentLoop + 1, nextNumber + 1);
  76.             if (resultFound)
  77.             {
  78.                 return;
  79.             }
  80.         }
  81.     }
  82.  
  83.     private static void CheckSubset()
  84.     {
  85.         int totalProtein = 0;
  86.         int totalCarboHydrats = 0;
  87.         int totalFat = 0;
  88.  
  89.         for (int index = 0; index < loops.Length; index++)
  90.         {
  91.             totalProtein += food[loops[index], 0];
  92.             totalCarboHydrats += food[loops[index], 1];
  93.             totalFat += food[loops[index], 2];
  94.         }
  95.  
  96.         if (totalProtein == int.Parse(targetCalories[0]) &&
  97.             totalCarboHydrats == int.Parse(targetCalories[1]) &&
  98.             totalFat == int.Parse(targetCalories[2]))
  99.         {
  100.             resultFound = true;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement