-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathEx_1_4_02.java
44 lines (39 loc) · 1.09 KB
/
Ex_1_4_02.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
package Ch_1_4;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.math.BigInteger;
/**
* Created by HuGuodong on 2019/8/7.
*/
public class Ex_1_4_02 {
public static int count(int[] a) {
int N = a.length;
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
for (int k = j + 1; k < N; k++) {
BigInteger sum = BigInteger.valueOf(0);
sum = sum.add(BigInteger.valueOf(a[i]));
sum = sum.add(BigInteger.valueOf(a[j]));
sum = sum.add(BigInteger.valueOf(a[k]));
if (sum == BigInteger.valueOf(0)) {
StdOut.printf("%d %d %d\n", a[i], a[j], a[k]);
cnt++;
}
}
}
}
return cnt;
}
public static void main(String[] args) {
// /Users/gdhu/projects/Algorithms4/algdata/1Kints.txt
// input 1Kints.txt
String file = "algdata/1Kints.txt";
int[] a = new In(file).readAllInts();
StdOut.println(count(a));
// 2147483647 -2147483647 0
// -2147483647 0 2147483647
// 2
}
}