-
Notifications
You must be signed in to change notification settings - Fork 1
/
#1010_Number Triangles
43 lines (37 loc) · 1.23 KB
/
#1010_Number Triangles
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
import java.util.Scanner;
/**
* Created by mingy on 2016/3/22.
*/
//这次就全是自己写的了,什么都没有参考,一次就很AC了很愉悦,用DP挺简单的
public class Quiz {
public static int maxOfArray(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; ++i)
if (max < a[i])
max = a[i];
return max;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int n = input.nextInt();
int[][] x = new int[n + 2][n + 2];
int[][] f = new int[n + 2][n + 2];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
x[i][j] = input.nextInt();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
f[i][j] = x[i][j];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
f[i][j] = x[i][j] + Math.max(f[i - 1][j], f[i - 1][j - 1]);
}
}
System.out.print(maxOfArray(f[n]));
/* for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j)
System.out.print(f[i][j] + " ");
System.out.println();
}*/
}
}