forked from chicuongit913/MumTestJavaExam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOddEven.java
62 lines (45 loc) · 1.03 KB
/
OddEven.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package mumTestPackage;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class MumTest2 {
@Test
public void main() {
int a1[] = {1};
assertEquals(f(a1), 1);
int f2[] = {1, 2};
assertEquals(f(f2), -1);
int f3[] = {1, 2, 3};
assertEquals(f(f3), 2);
int f4[] = {1, 2, 3, 4};
assertEquals(f(f4), -2);
int f5[] = {3, 3, 4, 4};
assertEquals(f(f5), -2);
int f6[] = {3, 2, 3, 4};
assertEquals(f(f6), 0);
int f7[] = {4, 1, 2, 3};
assertEquals(f(f7), -2);
int f8[] = {1, 1};
assertEquals(f(f8), 2);
int f9[] = {};
assertEquals(f(f9), 0);
}
int f(int[] a) {
if (a == null || a.length == 0) {
return 0;
}
int sumOdd = 0;
int sumEven = 0;
for (int i = 0; i < a.length; i++) {
//even number
if(a[i] % 2 == 0) {
sumEven += a[i];
} else { //odd number
sumOdd += a[i];
}
}
return sumOdd-sumEven;
}
}