SHOW:
|
|
- or go back to the newest paste.
1 | import java.util.Scanner; | |
2 | ||
3 | public class ArrayManipulator { | |
4 | public static void main(String[] args) { | |
5 | Scanner scanner = new Scanner(System.in); | |
6 | ||
7 | String[] inputAsArray = scanner.nextLine().split("\\s+"); | |
8 | String command = scanner.nextLine(); | |
9 | ||
10 | while (!"end".equals(command)) { | |
11 | String[] tokens = command.split("\\s+"); | |
12 | ||
13 | if (tokens[0].equals("exchange")) { | |
14 | inputAsArray = getNewArray(inputAsArray, tokens[1]); | |
15 | } else if (tokens[0].equals("max")) { | |
16 | if (tokens[1].equals("even")) { | |
17 | getMaxEven(inputAsArray); | |
18 | } else { | |
19 | getMaxOdd(inputAsArray); | |
20 | } | |
21 | } else if (tokens[0].equals("min")) { | |
22 | if (tokens[1].equals("even")) { | |
23 | getMinEven(inputAsArray); | |
24 | } else { | |
25 | getMinOdd(inputAsArray); | |
26 | } | |
27 | } else if (tokens[0].equals("first")) { | |
28 | if (tokens[2].equals("even")) { | |
29 | findFirstEven(inputAsArray, tokens[1]); | |
30 | } else { | |
31 | findFirstOdd(inputAsArray, tokens[1]); | |
32 | } | |
33 | } else if (tokens[0].equals("last")) { | |
34 | if (tokens[2].equals("even")) { | |
35 | findLastEven(inputAsArray, tokens[1]); | |
36 | } else { | |
37 | findLastOdd(inputAsArray, tokens[1]); | |
38 | } | |
39 | } | |
40 | command = scanner.nextLine(); | |
41 | } | |
42 | System.out.print("["); | |
43 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
44 | if (i == inputAsArray.length - 1) { | |
45 | System.out.print(inputAsArray[i]); | |
46 | } else { | |
47 | System.out.print(inputAsArray[i] + ", "); | |
48 | } | |
49 | } | |
50 | System.out.println("]"); | |
51 | } | |
52 | ||
53 | ||
54 | private static void findLastOdd(String[] inputAsArray, String token) { | |
55 | int neededCount = Integer.parseInt(token); | |
56 | if (neededCount > inputAsArray.length) { | |
57 | System.out.println("Invalid count"); | |
58 | } else { | |
59 | int countEven = 0; | |
60 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
61 | if (Integer.parseInt(inputAsArray[i]) % 2 != 0) { | |
62 | countEven++; | |
63 | } | |
64 | } | |
65 | int[] array = new int[Math.min(countEven, neededCount)]; | |
66 | if (array.length > 0) { | |
67 | int index = 0; | |
68 | for (int i = inputAsArray.length - 1; i >= 0; i--) { | |
69 | if (Integer.parseInt(inputAsArray[i]) % 2 != 0) { | |
70 | array[index] = Integer.parseInt(inputAsArray[i]); | |
71 | index++; | |
72 | if (index > array.length - 1) { | |
73 | break; | |
74 | } | |
75 | } | |
76 | } | |
77 | int[] reversedArray = new int[array.length]; | |
78 | for (int i = 0; i <= reversedArray.length - 1; i++) { | |
79 | reversedArray[i] = array[array.length - (i + 1)]; | |
80 | } | |
81 | ||
82 | System.out.print("["); | |
83 | for (int i = 0; i <= reversedArray.length - 1; i++) { | |
84 | if (i == reversedArray.length - 1) { | |
85 | System.out.print(reversedArray[i]); | |
86 | } else { | |
87 | System.out.print(reversedArray[i] + ", "); | |
88 | } | |
89 | } | |
90 | System.out.println("]"); | |
91 | } else { | |
92 | System.out.println("[]"); | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
97 | ||
98 | private static void findLastEven(String[] inputAsArray, String token) { | |
99 | int neededCount = Integer.parseInt(token); | |
100 | if (neededCount > inputAsArray.length) { | |
101 | System.out.println("Invalid count"); | |
102 | } else { | |
103 | int countEven = 0; | |
104 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
105 | if (Integer.parseInt(inputAsArray[i]) % 2 == 0) { | |
106 | countEven++; | |
107 | } | |
108 | } | |
109 | int[] array = new int[Math.min(countEven, neededCount)]; | |
110 | if (array.length > 0) { | |
111 | int index = 0; | |
112 | for (int i = inputAsArray.length - 1; i >= 0; i--) { | |
113 | if (Integer.parseInt(inputAsArray[i]) % 2 == 0) { | |
114 | array[index] = Integer.parseInt(inputAsArray[i]); | |
115 | index++; | |
116 | if (index > array.length - 1) { | |
117 | break; | |
118 | } | |
119 | } | |
120 | } | |
121 | int[] reversedArray = new int[array.length]; | |
122 | for (int i = 0; i <= reversedArray.length - 1; i++) { | |
123 | reversedArray[i] = array[array.length - (i + 1)]; | |
124 | } | |
125 | ||
126 | System.out.print("["); | |
127 | for (int i = 0; i <= reversedArray.length - 1; i++) { | |
128 | if (i == reversedArray.length - 1) { | |
129 | System.out.print(reversedArray[i]); | |
130 | } else { | |
131 | System.out.print(reversedArray[i] + ", "); | |
132 | } | |
133 | } | |
134 | System.out.println("]"); | |
135 | } else { | |
136 | System.out.println("[]"); | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | ||
142 | private static void findFirstOdd(String[] inputAsArray, String token) { | |
143 | int neededCount = Integer.parseInt(token); | |
144 | if (neededCount > inputAsArray.length) { | |
145 | System.out.println("Invalid count"); | |
146 | } else { | |
147 | int countOdd = 0; | |
148 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
149 | if (Integer.parseInt(inputAsArray[i]) % 2 != 0) { | |
150 | countOdd++; | |
151 | } | |
152 | } | |
153 | int[] array = new int[Math.min(countOdd, neededCount)]; | |
154 | if (array.length > 0) { | |
155 | int index = 0; | |
156 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
157 | if (Integer.parseInt(inputAsArray[i]) % 2 != 0) { | |
158 | array[index] = Integer.parseInt(inputAsArray[i]); | |
159 | index++; | |
160 | if (index > array.length - 1) { | |
161 | break; | |
162 | } | |
163 | } | |
164 | } | |
165 | System.out.print("["); | |
166 | for (int i = 0; i <= array.length - 1; i++) { | |
167 | if (i == array.length - 1) { | |
168 | System.out.print(array[i]); | |
169 | } else { | |
170 | System.out.print(array[i] + ", "); | |
171 | } | |
172 | } | |
173 | System.out.println("]"); | |
174 | } else { | |
175 | System.out.println("[]"); | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | ||
181 | private static void findFirstEven(String[] inputAsArray, String token) { | |
182 | int neededCount = Integer.parseInt(token); | |
183 | if (neededCount > inputAsArray.length) { | |
184 | System.out.println("Invalid count"); | |
185 | } else { | |
186 | int countEven = 0; | |
187 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
188 | if (Integer.parseInt(inputAsArray[i]) % 2 == 0) { | |
189 | countEven++; | |
190 | } | |
191 | } | |
192 | int[] array = new int[Math.min(countEven, neededCount)]; | |
193 | if (array.length > 0) { | |
194 | int index = 0; | |
195 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
196 | if (Integer.parseInt(inputAsArray[i]) % 2 == 0) { | |
197 | array[index] = Integer.parseInt(inputAsArray[i]); | |
198 | index++; | |
199 | if (index > array.length - 1) { | |
200 | break; | |
201 | } | |
202 | } | |
203 | } | |
204 | System.out.print("["); | |
205 | for (int i = 0; i <= array.length - 1; i++) { | |
206 | if (i == array.length - 1) { | |
207 | System.out.print(array[i]); | |
208 | } else { | |
209 | System.out.print(array[i] + ", "); | |
210 | } | |
211 | } | |
212 | System.out.println("]"); | |
213 | } else { | |
214 | System.out.println("[]"); | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | private static void getMinOdd(String[] inputAsArray) { | |
220 | int minElement = Integer.MAX_VALUE; | |
221 | int indexOfMinElement = -1; | |
222 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
223 | if (Integer.parseInt(inputAsArray[i]) % 2 != 0) { | |
224 | if (Integer.parseInt(inputAsArray[i]) <= minElement) { | |
225 | minElement = Integer.parseInt(inputAsArray[i]); | |
226 | indexOfMinElement = i; | |
227 | } | |
228 | } | |
229 | } | |
230 | if (indexOfMinElement > -1) { | |
231 | System.out.println(indexOfMinElement); | |
232 | } else { | |
233 | System.out.println("No matches"); | |
234 | } | |
235 | } | |
236 | ||
237 | private static void getMinEven(String[] inputAsArray) { | |
238 | int minElement = Integer.MAX_VALUE; | |
239 | int indexOfMinElement = -1; | |
240 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
241 | if (Integer.parseInt(inputAsArray[i]) % 2 == 0) { | |
242 | if (Integer.parseInt(inputAsArray[i]) <= minElement) { | |
243 | minElement = Integer.parseInt(inputAsArray[i]); | |
244 | indexOfMinElement = i; | |
245 | } | |
246 | } | |
247 | } | |
248 | if (indexOfMinElement > -1) { | |
249 | System.out.println(indexOfMinElement); | |
250 | } else { | |
251 | System.out.println("No matches"); | |
252 | } | |
253 | } | |
254 | ||
255 | private static void getMaxEven(String[] inputAsArray) { | |
256 | int maxElement = Integer.MIN_VALUE; | |
257 | int indexOfMaxElement = -1; | |
258 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
259 | if (Integer.parseInt(inputAsArray[i]) % 2 == 0) { | |
260 | if (Integer.parseInt(inputAsArray[i]) >= maxElement) { | |
261 | maxElement = Integer.parseInt(inputAsArray[i]); | |
262 | indexOfMaxElement = i; | |
263 | } | |
264 | } | |
265 | } | |
266 | if (indexOfMaxElement > -1) { | |
267 | System.out.println(indexOfMaxElement); | |
268 | } else { | |
269 | System.out.println("No matches"); | |
270 | } | |
271 | } | |
272 | ||
273 | private static void getMaxOdd(String[] inputAsArray) { | |
274 | int maxElement = Integer.MIN_VALUE; | |
275 | int indexOfMaxElement = -1; | |
276 | for (int i = 0; i <= inputAsArray.length - 1; i++) { | |
277 | if (Integer.parseInt(inputAsArray[i]) % 2 != 0) { | |
278 | if (Integer.parseInt(inputAsArray[i]) >= maxElement) { | |
279 | maxElement = Integer.parseInt(inputAsArray[i]); | |
280 | indexOfMaxElement = i; | |
281 | } | |
282 | } | |
283 | } | |
284 | if (indexOfMaxElement > -1) { | |
285 | System.out.println(indexOfMaxElement); | |
286 | } else { | |
287 | System.out.println("No matches"); | |
288 | } | |
289 | } | |
290 | ||
291 | ||
292 | private static String[] getNewArray(String[] inputAsArray, String token) { | |
293 | int index = Integer.parseInt(token); | |
294 | String[] newArray = new String[inputAsArray.length]; | |
295 | if (index < 0 || index >= inputAsArray.length) { | |
296 | System.out.println("Invalid index"); | |
297 | return inputAsArray; | |
298 | } else { | |
299 | int x = 0; | |
300 | for (int newStart = index + 1; newStart < inputAsArray.length; newStart++) { | |
301 | newArray[x] = inputAsArray[newStart]; | |
302 | x++; | |
303 | } | |
304 | for (int newEnd = 0; newEnd <= index; newEnd++) { | |
305 | newArray[x] = inputAsArray[newEnd]; | |
306 | x++; | |
307 | } | |
308 | ||
309 | } | |
310 | return newArray; | |
311 | } | |
312 | } |