Advertisement
YavorJS

SoftUni Water Supplies 50%

Sep 14th, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 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 Sample_Exam_II___June_2016
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. double totalWater = double.Parse(Console.ReadLine());
  14. double[] bottles = Console.ReadLine().Split().Select(double.Parse).ToArray();
  15. double capacity = double.Parse(Console.ReadLine());
  16.  
  17. int bottlesLeft = 0;
  18. double bottleDeficit = 0;
  19. bool even = true;
  20.  
  21. if (totalWater % 2 == 0)
  22. {
  23. even = true;
  24. for (int bottle = 0; bottle < bottles.Length; bottle++)
  25. {
  26. double bottleCapacity = bottles[bottle];
  27. while (bottleCapacity < capacity)
  28. {
  29. if (totalWater <= 0)
  30. {
  31. bottlesLeft = bottles.Length - bottle;
  32. bottleDeficit = capacity - bottleCapacity;
  33. bottle++;
  34. while (bottle < bottles.Length)
  35. {
  36. bottleCapacity = bottles[bottle];
  37. bottleDeficit += capacity - bottles[bottle];
  38. bottle++;
  39. }
  40. break;
  41. }
  42. if (capacity - bottleCapacity >= 1)
  43. {
  44. totalWater--;
  45. bottleCapacity++;
  46. }
  47. else
  48. {
  49. totalWater -= capacity - bottleCapacity;
  50. bottleCapacity = capacity;
  51. }
  52. }// end of while
  53. } // end of for
  54. } //end of if
  55.  
  56. else
  57. {
  58. even = false;
  59. for (int bottle = bottles.Length - 1; bottle >= 0; bottle--)
  60. {
  61. double bottleCapacity = bottles[bottle];
  62. while (bottleCapacity < capacity)
  63. {
  64. if (totalWater <= 0)
  65. {
  66. bottlesLeft = bottle + 1;
  67. bottleDeficit = capacity - bottleCapacity;
  68. bottle--;
  69. while (bottle >= 0)
  70. {
  71. bottleCapacity = bottles[bottle];
  72. bottleDeficit += capacity - bottleCapacity;
  73. bottle--;
  74. }
  75. break;
  76. }
  77. if (capacity - bottleCapacity >= 1)
  78. {
  79. totalWater--;
  80. bottleCapacity++;
  81. }
  82. else
  83. {
  84. totalWater -= capacity - bottleCapacity;
  85. bottleCapacity = capacity;
  86. }
  87. }// end of while
  88. }//end of for
  89. }//end of else
  90.  
  91. int[] remainingBottlesIndexes = new int[bottlesLeft];
  92.  
  93. if (even)
  94. {
  95. for (int bottle = bottlesLeft - 1; bottle >= 0; bottle--)
  96. {
  97. remainingBottlesIndexes[bottle] = bottles.Length - bottle;
  98. }
  99. remainingBottlesIndexes = remainingBottlesIndexes.Reverse().ToArray();
  100. }
  101. else
  102. {
  103.  
  104. for (int bottle = 0; bottle < bottlesLeft; bottle++)
  105. {
  106. remainingBottlesIndexes[bottle] = bottle;
  107. }
  108.  
  109. remainingBottlesIndexes = remainingBottlesIndexes.Reverse().ToArray();
  110. }
  111.  
  112.  
  113. if (bottlesLeft == 0)
  114. {
  115. Console.WriteLine("Enough water!");
  116. Console.WriteLine($"Water left: {totalWater}l.");
  117. }
  118. else
  119. {
  120. Console.WriteLine("We need more water!");
  121. Console.WriteLine($"Bottles left: {bottlesLeft}");
  122. Console.WriteLine("With indexes: " + string.Join(", ", remainingBottlesIndexes)); ;
  123. Console.WriteLine($"We need {bottleDeficit} more liters!");
  124. }
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement