Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. double amountOfWater = double.Parse(Console.ReadLine());
  10. double[] bottles = Console.ReadLine().Split().Select(double.Parse).ToArray();
  11. double bottleCapacity = double.Parse(Console.ReadLine());
  12. int indexOfLastFullBottle = 0;
  13. double neededWater = 0;
  14.  
  15. if (amountOfWater%2 == 0) //Starts from left to right
  16. {
  17. EvenWater(ref amountOfWater, bottles, bottleCapacity, ref indexOfLastFullBottle, ref neededWater);
  18. }
  19.  
  20. else // starts from the end
  21. OddWater(ref amountOfWater, bottles, bottleCapacity, ref indexOfLastFullBottle, ref neededWater);
  22. }
  23.  
  24. private static void OddWater(ref double amountOfWater, double[] bottles, double bottleCapacity,
  25. ref int indexOfLastFullBottle, ref double neededWater)
  26. {
  27. for (int i = bottles.Length - 1; i >= 0; i--)
  28. {
  29. double currentBottle = Math.Abs(bottleCapacity - (int) bottles[i]);
  30. amountOfWater = amountOfWater - currentBottle;
  31.  
  32. if (amountOfWater >= 0)
  33. {
  34. indexOfLastFullBottle = i;
  35. }
  36. else if (amountOfWater < 0)
  37. {
  38. neededWater = -amountOfWater;
  39. if (i != 0)
  40. {
  41. for (int j = indexOfLastFullBottle - 1; j >= 0; j--)
  42. {
  43. neededWater += (bottleCapacity - (int) bottles[j]);
  44. }
  45. }
  46. }
  47. }
  48. if (amountOfWater >= 0)
  49. {
  50. Console.WriteLine("Enough water!");
  51. Console.WriteLine("Water left: {0}l.", amountOfWater);
  52. }
  53. else
  54. {
  55. List<int> EmptyBottles = new List<int>();
  56. for (int i = indexOfLastFullBottle - 1; i >= 0; i--)
  57. {
  58. EmptyBottles.Add(i);
  59. }
  60. Console.WriteLine("We need more water!");
  61. Console.WriteLine("Bottles left: {0}", indexOfLastFullBottle);
  62. Console.WriteLine("With indexes: " + string.Join(", ", EmptyBottles));
  63. Console.WriteLine("We need {0} more liters!", neededWater);
  64. }
  65. }
  66.  
  67. private static void EvenWater(ref double amountOfWater, double[] bottles, double bottleCapacity,
  68. ref int indexOfLastFullBottle, ref double neededWater)
  69. {
  70. for (int i = 0; i < bottles.Length; i++)
  71. {
  72. double currentBottle = Math.Abs(bottleCapacity - bottles[i]);
  73. amountOfWater = amountOfWater - currentBottle;
  74.  
  75. if (amountOfWater >= 0)
  76. {
  77. indexOfLastFullBottle = i;
  78. neededWater = (bottles.Length - 1 - indexOfLastFullBottle)*bottleCapacity;
  79. }
  80. else if (amountOfWater < 0)
  81. {
  82. indexOfLastFullBottle = i - 1;
  83. neededWater = -amountOfWater;
  84. break;
  85. }
  86. }
  87. if (amountOfWater >= 0)
  88. {
  89. Console.WriteLine("Enough water!");
  90. Console.WriteLine("Water left: {0}l.", amountOfWater);
  91. }
  92. else
  93. {
  94. List<int> EmptyBottles = new List<int>();
  95. for (int i = 0; i < indexOfLastFullBottle; i++)
  96. {
  97. EmptyBottles.Add(i);
  98. }
  99. Console.WriteLine("We need more water!");
  100. Console.WriteLine("Bottles left: {0}", bottles.Length - 1 - indexOfLastFullBottle);
  101. Console.WriteLine("We need {0} more liters!", neededWater);
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement