Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ArrayManipulatorV2 {
  5.  
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String line = scanner.nextLine();
  9. int[] numbers = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray();
  10.  
  11. String input = "";
  12. while (!"end".equals(input = scanner.nextLine())) {
  13. String[] comands = input.split(" ");
  14.  
  15. if ("exchange".equals(comands[0])) {
  16. int index = Integer.parseInt(comands[1]);
  17. exchangesPlaces(numbers, index);
  18. } else if ("max".equals(comands[0])) {
  19. maxIndexElement(numbers, comands[1]);
  20. } else if ("min".equals(comands[0])) {
  21. minIndexElement(numbers, comands[1]);
  22. } else if ("first".equals(comands[0])) {
  23. int index = Integer.parseInt(comands[1]);
  24. firstElement(numbers, index, comands[2]);
  25. } else if ("last".equals(comands[0])) {
  26. int index = Integer.parseInt(comands[1]);
  27. lastElement(numbers, index, comands[2]);
  28. }
  29. }
  30. System.out.println(Arrays.toString(numbers));
  31. }
  32. private static void printArray(int count, int[] reverse) {
  33. if (count == 0) {
  34. System.out.println("[]");
  35. } else {
  36. String otput = "[";
  37. for (int i = 0; i < count; i++) {
  38. if (i < count - 1) {
  39. otput += (reverse[i]) + ", ";
  40.  
  41. } else {
  42. otput += (reverse[i]) + "]";
  43.  
  44. }
  45. }
  46. System.out.println(otput);
  47. }
  48. }
  49.  
  50. private static boolean invalidCoun(int[] numbers, int index) {
  51. if (index < 0 || index > numbers.length) {
  52. System.out.println("Invalid count");
  53. return true;
  54. }
  55. return false;
  56. }
  57. private static void lastElement(int[] numbers, int index, String comand) {
  58. int count = 0;
  59. int[] result = new int[numbers.length];
  60. if (invalidCoun(numbers, index)) return;
  61. if ("even".equals(comand)) {
  62. for (int i = 0; i < numbers.length; i++) {
  63. if (numbers[i] % 2 == 0 && index > count) {
  64. result[count++] = numbers[i];
  65. }
  66. }
  67. } else if ("odd".equals(comand)) {
  68. for (int i = 0; i < numbers.length; i++) {
  69. if (numbers[i] % 2 != 0 && index > count) {
  70. result[count++] = numbers[i];
  71. }
  72. }
  73. }
  74. int[] reverse = new int[result.length];
  75. for (int i = result.length-1; i >=0 ; i--) {
  76. reverse[i]=result[i];
  77. }
  78. printArray(count, reverse);
  79. }
  80.  
  81. private static void firstElement(int[] numbers, int index, String comand) {
  82. int count = 0;
  83. int[] result = new int[numbers.length];
  84. if (invalidCoun(numbers, index)) return;
  85. if ("even".equals(comand)) {
  86. for (int i = 0; i < numbers.length; i++) {
  87. if (numbers[i] % 2 == 0 && index > count) {
  88. result[count++] = numbers[i];
  89. }
  90. }
  91. } else if ("odd".equals(comand)) {
  92. for (int i = 0; i < numbers.length; i++) {
  93. if (numbers[i] % 2 != 0 && index > count) {
  94. result[count++] = numbers[i];
  95. }
  96. }
  97. }
  98. printArray(count, result);
  99. }
  100.  
  101. private static void minIndexElement(int[] numbers, String comand) {
  102. int min = Integer.MAX_VALUE;
  103. int index = -1;
  104. if ("even".equals(comand)) {
  105. for (int i = 0; i < numbers.length; i++) {
  106. if (numbers[i] % 2 == 0) {
  107. if (numbers[i] <= min) {
  108. min = numbers[i];
  109. index = i;
  110. }
  111. }
  112. }
  113. } else if ("odd".equals(comand)) {
  114. for (int i = 0; i < numbers.length; i++) {
  115. if (numbers[i] % 2 != 0) {
  116. if (numbers[i] <= min) {
  117. min = numbers[i];
  118. index = i;
  119. }
  120. }
  121. }
  122. }
  123. if (index == -1) {
  124. System.out.println("No matches");
  125. } else {
  126. System.out.println(index);
  127. }
  128. }
  129.  
  130. private static void maxIndexElement(int[] numbers, String comand) {
  131. int max = Integer.MIN_VALUE;
  132. int index = -1;
  133. if ("even".equals(comand)) {
  134. for (int i = 0; i < numbers.length; i++) {
  135. if (numbers[i] % 2 == 0) {
  136. if (numbers[i] >= max) {
  137. max = numbers[i];
  138. index = i;
  139. }
  140. }
  141. }
  142. } else if ("odd".equals(comand)) {
  143. for (int i = 0; i < numbers.length; i++) {
  144. if (numbers[i] % 2 != 0) {
  145. if (numbers[i] >= max) {
  146. max = numbers[i];
  147. index = i;
  148. }
  149. }
  150. }
  151. }
  152. if (index == -1) {
  153. System.out.println("No matches");
  154. } else {
  155. System.out.println(index);
  156. }
  157. }
  158.  
  159. private static void exchangesPlaces(int[] numbers, int index) {
  160. int count = 0;
  161. int[] result = new int[numbers.length];
  162. if (index < 0 || index >= numbers.length) {
  163. System.out.println("Invalid index");
  164. return;
  165. }
  166. for (int i = index + 1; i < numbers.length; i++) {
  167. result[count++] = numbers[i];
  168. }
  169. for (int i = 0; i <= index; i++) {
  170. result[count++] = numbers[i];
  171. }
  172. for (int i = 0; i < numbers.length; i++) {
  173. numbers[i] = result[i];
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement