-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem13.java
executable file
·47 lines (36 loc) · 1.07 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
import java.util.*;
import java.io.File;
import java.io.IOException;
public class helloworld {
public static void main(String []args)
throws IOException {
//System.out.println("Hello World");
Scanner s = new Scanner(new File("hundred50digit.txt"));
System.out.println("Answer: " + Addition(s));
}
public static String Addition(Scanner s) {
String[] array = new String[100];
String current;
int index = 0;
int digitSum = 0;
StringBuffer result = new StringBuffer();
while (s.hasNext()) {
array[index] = s.next();
index++;
}
for (int i = 49; i >= 0; i--) {
for (int j = 0; j < array.length; j++) {
current = array[j];
digitSum += Character.getNumericValue(current.charAt(i));
}
String tmp = Integer.toString(digitSum);
result.append(tmp.charAt(tmp.length() - 1));
StringBuffer temp = new StringBuffer(tmp);
temp.deleteCharAt(temp.length() - 1);
digitSum = Integer.parseInt(temp.toString());
}
result.append(Integer.toString(digitSum));
result.reverse();
return result.substring(0, 10);
}
}