-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler67.java
More file actions
66 lines (56 loc) · 1.49 KB
/
euler67.java
File metadata and controls
66 lines (56 loc) · 1.49 KB
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
//Using an efficient algorithm find the maximal sum in the triangle
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
class Euler67
{
public static void main(String argz[])
{
int siz = 100+1;
int tri[][]= new int[siz][siz];
int dyn[][]= new int[siz+1][siz+1];
try{
BufferedReader in = new BufferedReader(new FileReader(argz[0]));
String currline;
try
{
while((currline = in.readLine()) != null)
{
String temp[] = currline.split(" ");
for(int i=0;i<temp.length;i++)
{
tri[temp.length-1-i+1][i+1] = Integer.parseInt(temp[i]);
}
}
} catch (IOException e) { e.printStackTrace();}
} catch(FileNotFoundException e) { e.printStackTrace(); }
int largest = 0;
for(int i=1;i<siz;i++)
{
for(int j=1;j<siz+1-i;j++)
{
if(dyn[i-1][j] != 0 && dyn[i][j-1] != 0){
if (dyn[i-1][j] > dyn[i][j-1]){
dyn[i][j] = tri[i][j] + dyn[i-1][j];
}
else{
dyn[i][j] = tri[i][j] + dyn[i][j-1];
}
}
else if(dyn[i-1][j]!=0){
dyn[i][j] = tri[i][j] + dyn[i-1][j];
}
else if(dyn[i][j-1] != 0){
dyn[i][j] = tri[i][j] + dyn[i][j-1];
}
else {
dyn[i][j] = tri[i][j];
}
if (dyn[i][j] > largest)
largest = dyn[i][j];
}
}
System.out.println(largest);
}
}