Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class StartUp
  6. {
  7. static void Main()
  8. {
  9. try
  10. {
  11. var sequanceOfElements = Console.ReadLine()
  12. .Split()
  13. .Select(int.Parse)
  14. .ToList();
  15. if (sequanceOfElements.Count == 0 || sequanceOfElements.Count == 1)
  16. {
  17. return;
  18. }
  19.  
  20. var listOfLosers = new List<int>();
  21. var listOfWinners = new List<int>();
  22.  
  23. for (int index = 0; index < sequanceOfElements.Count; index++)
  24. {
  25. if (!listOfLosers.Contains(index))
  26. {
  27. int target = sequanceOfElements[index];
  28. if (target >= sequanceOfElements.Count)
  29. {
  30. target %= sequanceOfElements.Count;
  31. }
  32.  
  33. var difference = Math.Abs(index - target);
  34.  
  35. if (difference % 2 == 0 && difference != 0)
  36. {
  37. //attacker wins
  38.  
  39. listOfWinners.Add(index);
  40. if (!listOfLosers.Contains(target))
  41. {
  42. listOfLosers.Add(target);
  43. }
  44.  
  45. if (!listOfLosers.Contains(index))
  46. {
  47. Console.WriteLine($"{index} x {target} -> {index} wins");
  48. }
  49. }
  50. else if (difference % 2 == 1)
  51. {
  52. //target wins
  53.  
  54. if (!listOfLosers.Contains(index))
  55. {
  56. listOfLosers.Add(index);
  57. }
  58.  
  59. Console.WriteLine($"{index} x {target} -> {target} wins");
  60. }
  61. else if (difference == 0)
  62. {
  63. //attackerElement makes harakiri
  64.  
  65. if (!listOfLosers.Contains(index))
  66. {
  67. listOfLosers.Add(index);
  68. }
  69.  
  70. Console.WriteLine($"{index} performed harakiri");
  71. }
  72. //stop program if only 1 element left
  73.  
  74. if (sequanceOfElements.Count - listOfLosers.Count == 1)
  75. {
  76. break;
  77. }
  78. }
  79. //check if we make all iterations
  80.  
  81. if (index + 1 == sequanceOfElements.Count)
  82. {
  83. var resultSequance = new List<int>();
  84. foreach (var element in listOfWinners)
  85. {
  86. var currentEl = sequanceOfElements.ElementAt(element);
  87.  
  88. resultSequance.Add(currentEl);
  89. }
  90.  
  91. listOfWinners.Clear();
  92. listOfLosers.Clear();
  93. sequanceOfElements = resultSequance;
  94.  
  95. index = -1;
  96. }
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement