Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem2
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int countCommands = int.Parse(Console.ReadLine());
  12.  
  13. List<int> numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
  14.  
  15. int position = 0;
  16.  
  17. for (int i = 0; i < countCommands; i++)
  18. {
  19. string line = Console.ReadLine();
  20.  
  21. string[] lineParts = line.Split();
  22.  
  23. string command = lineParts[0];
  24. if (command == "Forward")
  25. {
  26. int stepsForward = int.Parse(lineParts[1]);
  27.  
  28. position += stepsForward;
  29. if (position >= 0 && position < numbers.Count)
  30. {
  31. numbers.RemoveAt(position);
  32. }
  33. else
  34. {
  35. position -= stepsForward;
  36. }
  37.  
  38.  
  39.  
  40. }
  41. else if (command == "Back")
  42. {
  43. int stepsBack = int.Parse(lineParts[1]);
  44.  
  45.  
  46. position -= stepsBack;
  47. if (position >= 0 && position < numbers.Count)
  48. {
  49. numbers.RemoveAt(position);
  50. }
  51. else
  52. {
  53. position -= stepsBack;
  54. }
  55.  
  56. }
  57. else if (command == "Gift")
  58. {
  59. int index = int.Parse(lineParts[1]);
  60.  
  61. if (index >= 0 && index < numbers.Count)
  62. {
  63. int numberToInsert = int.Parse(lineParts[2]);
  64.  
  65. numbers.Insert(index, numberToInsert);
  66.  
  67. position = index;
  68. }
  69.  
  70.  
  71. }
  72. else if (command == "Swap")
  73. {
  74. int firstNum = int.Parse(lineParts[1]);
  75. int secondNum = int.Parse(lineParts[2]);
  76.  
  77. if (numbers.Contains(firstNum) && numbers.Contains(secondNum))
  78. {
  79. int indexFirstNum = numbers.IndexOf(firstNum);
  80. int indexSecondNum = numbers.IndexOf(secondNum);
  81.  
  82. numbers[indexFirstNum] = secondNum;
  83. numbers[indexSecondNum] = firstNum;
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90. Console.WriteLine($"Position: {position}");
  91. Console.WriteLine(string.Join(", ", numbers));
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement