Advertisement
sivancheva

SoftUniWaterSupplies2

Oct 9th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace _02_SoftUniWaterSupplies
  7. {
  8. class SoftUniWaterSupplies
  9. {
  10. static void Main(string[] args)
  11. {
  12. decimal waterTotal = decimal.Parse(Console.ReadLine());
  13. var array = Console.ReadLine().Split(' ').Select(decimal.Parse).ToArray();
  14. decimal capacity = decimal.Parse(Console.ReadLine());
  15.  
  16.  
  17. var currIndexes = new List<int>();
  18. var currFilledBottles = new List<decimal>(array);
  19.  
  20.  
  21. decimal waterCurrent = waterTotal;
  22.  
  23. var waterToFill = 0m;
  24.  
  25. if (waterTotal % 2 == 0)
  26. {
  27. for (int i = 0; i < array.Length; i++)
  28. {
  29. if (waterCurrent < 0)
  30. {
  31. break;
  32. }
  33. waterToFill = capacity - array[i];
  34.  
  35. waterCurrent -= waterToFill;
  36.  
  37. if (waterCurrent >= 0)
  38. {
  39. currFilledBottles[i] = currFilledBottles[i] + waterToFill;
  40. }
  41. else
  42. {
  43. currFilledBottles[i] = currFilledBottles[i] + (waterToFill + waterCurrent);
  44. }
  45. }
  46. }
  47. else
  48. {
  49. int counter = 1;
  50. for (int i = array.Length - 1; i >= 0; i--)
  51. {
  52.  
  53. if (waterCurrent < 0)
  54. {
  55. break;
  56. }
  57. waterToFill = capacity - array[i];
  58.  
  59. waterCurrent -= waterToFill;
  60.  
  61. if (waterCurrent >= 0)
  62. {
  63. currFilledBottles[array.Length - counter] = currFilledBottles[array.Length - counter] + waterToFill;
  64. counter++;
  65. }
  66. else
  67. {
  68. currFilledBottles[array.Length - counter] = currFilledBottles[array.Length - counter] + (waterToFill + waterCurrent);
  69. }
  70. }
  71. }
  72.  
  73. for (int i = 0; i < array.Length; i++)
  74. {
  75. if (currFilledBottles[i] < capacity)
  76. {
  77. currIndexes.Add(i);
  78. }
  79. }
  80. int bottlesCount = currFilledBottles.Where(x => x < capacity).Count();
  81.  
  82. if (waterCurrent >= 0)
  83. {
  84. Console.WriteLine("Enough water!");
  85. Console.WriteLine($"Water left: {waterCurrent}l.");
  86. }
  87. else
  88. {
  89. currIndexes.Reverse();
  90.  
  91. currFilledBottles = currFilledBottles.Where(x => x != capacity).ToList();
  92. var nededWater = capacity * currFilledBottles.Count - currFilledBottles.Sum();
  93.  
  94. Console.WriteLine("We need more water!");
  95. Console.WriteLine($"Bottles left: {bottlesCount}");
  96. Console.Write("With indexes: ");
  97. Console.WriteLine(String.Join(", ", currIndexes));
  98. Console.WriteLine($"We need {nededWater} more liters!");
  99. }
  100.  
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement