viraco4a

JuiceDietDictionaries

Feb 27th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _08_diet
  8. {
  9. class Program
  10. {
  11. static void Main()
  12. {
  13. int Raspberries = int.Parse(Console.ReadLine());
  14. int Strawberries = int.Parse(Console.ReadLine());
  15. int Cherries = int.Parse(Console.ReadLine());
  16. int neededJuice = int.Parse(Console.ReadLine());
  17. var fruitJuice = new Dictionary<string, double>()
  18. {
  19. { "Raspberries", 4.5 }, { "Strawberries", 7.5 }, { "Cherries", 15 }
  20. };
  21. var totalCombinations = new List<Dictionary<string, int>>(1024);
  22.  
  23. double local = 0;
  24. double max = 0;
  25. int maxCherries = Math.Min(Cherries, neededJuice / 15);
  26. int maxStrawberries = Math.Min(Strawberries, (int)(neededJuice / 7.5));
  27. int maxRaspberries = Math.Min(Raspberries, (int)(neededJuice / 4.5));
  28.  
  29. for (int i = 0; i <= Raspberries; i++)
  30. {
  31. for (int j = 0; j <= maxStrawberries; j++)
  32. {
  33. for (int k = 0; k <= maxCherries; k++)
  34. {
  35. local = i * fruitJuice["Raspberries"] + j * fruitJuice["Strawberries"] + k * fruitJuice["Cherries"];
  36. if (local <= neededJuice && local >= max)
  37. {
  38. max = local;
  39. var tmp = new Dictionary<string, int>()
  40. {
  41. { "Raspberries", i }, { "Strawberries", j }, { "Cherries", k }
  42. };
  43. totalCombinations.Add(tmp);
  44. }
  45. if (local > neededJuice)
  46. {
  47. break;
  48. }
  49. }
  50. }
  51. }
  52.  
  53.  
  54. foreach (var combination in totalCombinations)
  55. {
  56. double total = 0;
  57. foreach (var item in combination)
  58. {
  59. string fruit = item.Key;
  60. int quantity = item.Value;
  61. total += quantity * fruitJuice[fruit];
  62. }
  63. if (total == max)
  64. {
  65. Console.WriteLine($"{combination["Raspberries"]} Raspberries, {combination["Strawberries"]} Strawberries," +
  66. $" {combination["Cherries"]} Cherries. Juice: {max} ml.");
  67. break;
  68. }
  69. }
  70.  
  71. }
  72. }
  73. }
Add Comment
Please, Sign In to add comment