标签:
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
30
1 for(int i=num-2;i>=0;i--) //核心 简单的动規; 2 { 3 for(int j=0;j<=i;j++) 4 { 5 dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1]); 6 } 7 }
1 #include<stdio.h> 2 int main() 3 { 4 int n,i,j,num[110][110]; 5 scanf("%d",&n); 6 for(i=0;i<n;i++) 7 for(j=0;j<=i;j++) 8 scanf("%d",&num[i][j]); 9 for(i=n-2;i>=0;i--) 10 for(j=0;j<=i;j++) 11 { 12 num[i][j]+=num[i+1][j]>num[i+1][j+1]?num[i+1][j]:num[i+1][j+1]; 13 } 14 printf("%d\n",num[0][0]); 15 return 0; 16 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4579513.html