bullit3189

GrainsOfSands - MidExam-27Aug18

Jan 29th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _02GrainsOfSands
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<int> sands = Console.ReadLine().Split().Select(int.Parse).ToList();
  12.  
  13. while (true)
  14. {
  15. string command = Console.ReadLine();
  16.  
  17. if (command == "Mort")
  18. {
  19. break;
  20. }
  21.  
  22. string[] tokens = command.Split();
  23.  
  24. string action = tokens[0];
  25.  
  26. if (action == "Add")
  27. {
  28. int value = int.Parse(tokens[1]);
  29.  
  30. sands.Add(value);
  31. }
  32. else if (action == "Remove")
  33. {
  34. int value = int.Parse(tokens[1]);
  35.  
  36. if (sands.Contains(value))
  37. {
  38. sands.Remove(value);
  39. }
  40. else
  41. {
  42. int index = value;
  43. if (index>=0 && index<=sands.Count-1)
  44. {
  45. sands.RemoveAt(index);
  46. }
  47. }
  48. }
  49. else if (action == "Replace")
  50. {
  51. int value = int.Parse(tokens[1]);
  52. int replacement = int.Parse(tokens[2]);
  53.  
  54. for (int i = 0; i < sands.Count; i++)
  55. {
  56. int currNum = sands[i];
  57.  
  58. if (currNum == value)
  59. {
  60. sands[i] = replacement;
  61. break;
  62. }
  63.  
  64. }
  65. }
  66. else if (action == "Increase")
  67. {
  68. int value = int.Parse(tokens[1]);
  69.  
  70. bool isFound = false;
  71.  
  72. for (int i = 0; i < sands.Count; i++)
  73. {
  74. int currNum = sands[i];
  75.  
  76. if (currNum>=value)
  77. {
  78. isFound = true;
  79.  
  80. for (int j = 0; j < sands.Count; j++)
  81. {
  82. sands[j] += currNum;
  83. }
  84.  
  85. break;
  86. }
  87.  
  88. }
  89.  
  90. if (isFound == false)
  91. {
  92. int valueToIncrease = sands[sands.Count - 1];
  93.  
  94. for (int i = 0; i < sands.Count; i++)
  95. {
  96. sands[i] += valueToIncrease;
  97. }
  98. }
  99. }
  100. else if (action == "Collapse")
  101. {
  102. int value = int.Parse(tokens[1]);
  103.  
  104. sands = sands.Where(x => x >= value).ToList();
  105. }
  106. }
  107. Console.WriteLine(string.Join(" ",sands));
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment