1
2
3
4
5
6
7
8
9
10
11
12class Field {
13
14 public static
15 void main(String[] args) {
16
17
18 {
19 long [] a1 = {};
20 Array f1 = new Array(a1);
21
22 f1.testCases("f1");
23 }
24
25
26 {
27 long [] a2 = {1};
28 Array f2 = new Array(a2);
29
30 f2.testCases("f2");
31 }
32
33 {
34 long [] a3 = {1, 100, 40, 40, 100, 3};
35 Array f3 = new Array(a3);
36
37 f3.testCases("f3");
38 }
39 }
40}
41
42
43
44
45
46
47
48
49class Array {
50
51
52
53
54
55 private
56 long [] f;
57
58
59
60
61
62
63 public
64 Array(long [] f) {
65 this.f = f;
66 }
67
68
69
70
71
72
73 public
74 long at(int i) {
75 return
76 f[i];
77 }
78
79
80
81
82
83
84 public
85 void putAt(int i, long v) {
86 f[i] = v;
87 }
88
89
90
91
92
93
94
95 public
96 boolean contains(long v) {
97 return
98 contains(v, f.length);
99 }
100
101
102
103
104
105
106
107
108
109
110 public
111 boolean contains(long v,
112 int n) {
113 int i = 0;
114
115 while ( i < n && f[i] != v ) {
116 i = i + 1;
117 }
118
119 return
120 i < n;
121 }
122
123
124
125
126
127
128 public
129 boolean allEqual() {
130 return
131 allEqual(f.length);
132 }
133
134
135
136
137
138
139
140
141 public
142 boolean allEqual(int n) {
143 int i = 1;
144 boolean equal = true;
145
146 while ( equal && i < n ) {
147 equal = f[i-1] == f[i];
148 i = i + 1;
149 }
150
151 return
152 equal;
153 }
154
155
156
157
158
159
160 public
161 boolean allLessThanOrEqualTo(long m) {
162 return
163 allLessThanOrEqualTo(m, f.length);
164 }
165
166
167
168
169
170
171
172
173
174
175 public
176 boolean allLessThanOrEqualTo(long m,
177 int n) {
178 int i = 0;
179
180 while ( i < n && f[i] <= m) {
181 i = i + 1;
182 }
183
184 return
185 i == n;
186 }
187
188
189
190
191
192
193
194
195 public
196 boolean allExceptOneLessThanOrEqualTo(long m2,
197 long m) {
198 return
199 allExceptOneLessThanOrEqualTo(m2,m,f.length);
200 }
201
202
203
204
205
206
207
208
209
210 public boolean allExceptOneLessThanOrEqualTo(long m2,
211 long m,
212 int n) {
213 int i = 0;
214 boolean r = true;
215
216 while ( r && i < n ) {
217 r = (f[i] == m) || (f[i] <= m2);
218 i = i + 1;
219 }
220
221 return r;
222 }
223
224
225
226
227
228
229
230 public long max() {
231 return
232 max(f.length);
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246 public long max(int n) {
247 int i = 1;
248 long m = f[0];
249
250 while ( i < n ) {
251 m = f[i] > m ? f[i] : m;
252 i = i + 1;
253 }
254
255 return m;
256 }
257
258
259
260
261
262
263 public long max2() {
264 return
265 max2(f.length);
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 public
288 long max2(int n) {
289 int i = 1;
290 long max = f[0];
291 long max2 = max;
292
293 while ( i < n ) {
294
295
296 if ( f[i] > max ) {
297 max2 = max;
298 max = f[i];
299 }
300 else
301 if ( f[i] < max ) {
302
303 if ( max == max2 ) {
304
305 max2 = f[i];
306 }
307 else {
308
309 if ( f[i] > max2 ) {
310 max2 = f[i];
311 }
312 }
313 }
314 else {
315
316
317 }
318 i = i + 1;
319 }
320
321 return max2;
322 }
323
324
325
326
327
328
329 public
330 String toString() {
331 if (f.length == 0)
332 return
333 "{}";
334 else {
335 String r = "{";
336
337 for (int i = 0;
338 i < f.length;
339 ++i) {
340 r = r + f[i] + (i < f.length -1 ? "," : "}");
341 }
342
343 return r;
344 }
345 }
346
347
348
349
350
351
352 public
353 void testCases(String n) {
354 System.out.println("Array " + n + " = " + toString());
355 System.out.println(n + ".length = " + f.length);
356 System.out.println(n + ".contains(0) = " + contains(0));
357 System.out.println(n + ".contains(1) = " + contains(1));
358 System.out.println(n + ".allEqual() = " + allEqual());
359
360 if (f.length != 0) {
361 System.out.println(n + ".max() = " + max());
362 System.out.println(n + ".max2() = " + max2());
363 System.out.println(n + ".max() == " + n + ".max2() = " +
364 (max() == max2()));
365 System.out.println(n + ".contains(" + f[0] + ") = " + contains(f[0]));
366 System.out.println(n + ".contains(" + max() + ") = " + contains(max()));
367 System.out.println(n + ".contains(" + max2() + ") = " + contains(max2()));
368 System.out.println(n + ".allLessThanOrEqualTo(" + f[0] + ") = " +
369 allLessThanOrEqualTo(f[0]));
370 System.out.println(n + ".allLessThanOrEqualTo(" + max() + ") = " +
371 allLessThanOrEqualTo(max()));
372 System.out.println(n + ".allLessThanOrEqualTo(" + max2() + ") = " +
373 allLessThanOrEqualTo(max2()));
374 System.out.println(n + ".allExceptOneLessThanOrEqualTo(" + max2() + "," + max() + ") = " +
375 allExceptOneLessThanOrEqualTo(max2(),max()));
376 System.out.println(n + ".allExceptOneLessThanOrEqualTo(" + max() + "," + max2() + ") = " +
377 allExceptOneLessThanOrEqualTo(max(),max2()));
378 }
379 System.out.println("");
380 }
381
382}
383
384