-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem13.java
66 lines (53 loc) · 1.3 KB
/
Problem13.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
63
64
65
66
import java.io.*;
import java.util.*;
public class Problem13 {
private static final int NUMS = 100;
private static final int NUM_LEN = 50;
private static final int LEADING = 2;
public static void main(String[] args) {
new Problem13().start();
}
public void start() {
String[] numbers = readNums();
int[] result = new int[NUM_LEN + LEADING];
int sum = 0;
int carry100, carry10, base;
for (int j = NUM_LEN - 1; j >= 0; j--) {
sum = 0;
for (int i = 0; i < NUMS; i++) {
sum += Character.getNumericValue(numbers[i].charAt(j));
}
sum += result[j + LEADING];
carry100 = sum / 100;
sum %= 100;
carry10 = sum / 10;
sum %= 10;
base = sum;
result[j + LEADING] = base;
result[j + 1] += carry10;
result[j] += carry100;
}
for (int i = 0; i < NUM_LEN+LEADING; i++) {
System.out.print(result[i]);
}
}
private String[] readNums() {
Scanner scanner = null;
String[] numbers = new String[NUMS];
try {
scanner = new Scanner(new File("prob13_data.txt"));
int i = 0;
while (scanner.hasNext()) {
numbers[i++] = scanner.next();
}
} catch (FileNotFoundException fnfe) {
System.err.println("Could not open data file");
return null;
} finally {
if (scanner != null) {
scanner.close();
}
}
return numbers;
}
}