本文共 1175 字,大约阅读时间需要 3 分钟。
Description
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.
Input
Output
Sample Input
573 88 1 0 2 7 4 44 5 2 6 5
Sample Output
30 题目大意: 求从最上面到最下面的最大和;参照Figure 1图
#includeusing namespace std;int main(){ int n,aa[100][100]; cin>>n; for(int i=0;i >aa[i][j]; for(int i=n-2;i>=0;i--)//为什么要n-2呢?? //思路是从倒数第一行开始把值加到上面一行,这样得到的aa[0][0]一定是最大值 { for(int j=0;j<=i;j++) { aa[i][j]+=max(aa[i+1][j],aa[i+1][j+1]); } }cout< <
转载地址:http://pbqyx.baihongyu.com/