Advertisement
Guest User

problem1

a guest
Jun 1st, 2015
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections.Generic;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace Problem1
  10. {
  11. class Problem1
  12. {
  13. private static String[] _charArray;
  14.  
  15. static void Main(string[] args)
  16. {
  17. String str = Console.ReadLine().Trim();
  18. str = Regex.Replace(str, @"([ ]{2,})", " ");
  19. _charArray = str.Split(' ');
  20.  
  21. while (true)
  22. {
  23. String[] command = Console.ReadLine().Split(' ');
  24. if (command[0] == "end")
  25. {
  26. break;
  27. }
  28. //PrintResult();
  29. switch (command[0])
  30. {
  31. case "reverse":
  32. if (StartIsValid(int.Parse(command[2])) && CounIsValid(int.Parse(command[2]), int.Parse(command[4])))
  33. {
  34. //Console.WriteLine(int.Parse(command[4]));
  35. DoReverse(int.Parse(command[2]), int.Parse(command[4]));
  36. }
  37. else
  38. {
  39. PrintError();
  40. }
  41. break;
  42. case "sort":
  43. if (StartIsValid(int.Parse(command[2])) && CounIsValid(int.Parse(command[2]), int.Parse(command[4])))
  44. {
  45. DoSort(int.Parse(command[2]), int.Parse(command[4]));
  46. }
  47. else
  48. {
  49. PrintError();
  50. }
  51. break;
  52. case "rollLeft":
  53. if (IsValid(int.Parse(command[1])))
  54. {
  55. DoRollLeft(int.Parse(command[1]));
  56. }
  57. else
  58. {
  59. PrintError();
  60. }
  61.  
  62. break;
  63.  
  64. case "rollRight":
  65. if (IsValid(int.Parse(command[1])))
  66. {
  67. DoRollRight(int.Parse(command[1]));
  68. }
  69. else
  70. {
  71. PrintError();
  72. }
  73.  
  74. break;
  75. }
  76. }
  77.  
  78. PrintResult(_charArray);
  79. }
  80.  
  81. private static bool IsValid(int i)
  82. {
  83. return i >= 0;
  84. }
  85.  
  86. private static void DoRollRight(int count)
  87. {
  88. int length = _charArray.Length;
  89. int n = count % length;
  90.  
  91. String[] newArr = new String[_charArray.Length];
  92. for (int i = 0; i < newArr.Length; i++)
  93. {
  94. newArr[n] = _charArray[i];
  95. n++;
  96. if (n >= newArr.Length)
  97. {
  98. n = 0;
  99. }
  100. }
  101.  
  102. _charArray = newArr;
  103. }
  104.  
  105. private static void DoRollLeft(int count)
  106. {
  107. int length = _charArray.Length;
  108. int n = count % length;
  109.  
  110. if (n != 0)
  111. {
  112. n = _charArray.Length - n;
  113. }
  114.  
  115. String[] newArr = new String[_charArray.Length];
  116. for (int i = 0; i < newArr.Length; i++)
  117. {
  118. newArr[n] = _charArray[i];
  119. n++;
  120. if (n >= newArr.Length)
  121. {
  122. n = 0;
  123. }
  124. }
  125.  
  126. _charArray = newArr;
  127.  
  128. }
  129.  
  130. private static void DoSort(int start, int count)
  131. {
  132. String[] tmp = new String[count];
  133. int index = 0;
  134. int fIndex = start;
  135. for (int i = 0; i < count; i++)
  136. {
  137. tmp[i] = _charArray[fIndex];
  138. index++;
  139. fIndex++;
  140. }
  141.  
  142. Array.Sort(tmp);
  143.  
  144. index = 0;
  145. fIndex = start;
  146. for (int i = 0; i < count; i++)
  147. {
  148. _charArray[fIndex] = tmp[i];
  149. index++;
  150. fIndex++;
  151. }
  152. }
  153.  
  154. private static void DoReverse(int start, int count)
  155. {
  156. String[] tmp = new String[count];
  157. int index = 0;
  158.  
  159. int fIndex = start;
  160. for (int i = 0; i < count; i++)
  161. {
  162. tmp[i] = _charArray[fIndex];
  163. index++;
  164. fIndex++;
  165. }
  166.  
  167. //PrintResult(tmp);
  168. tmp = tmp.Reverse().ToArray();
  169. //PrintResult(tmp);
  170.  
  171. index = 0;
  172. fIndex = start;
  173. for (int i = 0; i < count; i++)
  174. {
  175. _charArray[fIndex] = tmp[i];
  176. index++;
  177. fIndex++;
  178. }
  179. }
  180.  
  181. private static bool StartIsValid(int start)
  182. {
  183. return (start >= 0) && (start < _charArray.Length);
  184. }
  185.  
  186. private static bool CounIsValid(int start, int length)
  187. {
  188. //return length > 0 && (length + (start + 1)) < _charArray.Length;
  189. return (start + length >= 0) && ((start + length) <= _charArray.Length);
  190. }
  191.  
  192. private static void PrintError()
  193. {
  194. Console.WriteLine("Invalid input parameters.");
  195. }
  196.  
  197. private static void PrintResult(String[] arr)
  198. {
  199. Console.Write("[");
  200. for (int i = 0; i < arr.Length; i++)
  201. {
  202. Console.Write(arr[i]);
  203. if (i != arr.Length - 1)
  204. {
  205. Console.Write(", ");
  206. }
  207. }
  208.  
  209. Console.WriteLine("]");
  210. }
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement