Advertisement
sivancheva

SoftUniWaterSupplies

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