Description
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 A number triangle is given. There are many different paths from the top of the triangle to the bottom. For each path, add up the number of paths above to get one and your task is to find the largest and. Note: Each step on the path can only go from one number to the next, or to the number on the right or to the nearest left.
Input
The input data has multiple groups, each input line is an integer N (1 < N <= 100), giving the number of rows of the triangle. The following n lines give the number triangles. The number on the number triangle ranges between 0 and 100.
Output
Outputs the maximum and.
Sample Input
573 88 1 0 2 7 4 44 5 2 6 5312 31 1 1
Sample Output
305
1#include <iostream>2 using namespacestd;3 intMain ()4 {5 intdata[ $][ $];//Defining Arrays6 intn,tmp;7 while(cin>>N)8 {9n=n+1;//It's a question, and I don't know why.Ten for(intI=0; i<n;i++) One { A for(intj=0; j<i;j++) -Cin>>Data[i][j]; - } the for(intI=0; i<n;i++) - { - for(intj=0; j<i;j++) - { + if(j==0)//Judging boundary problems -data[i][j]=data[i-1][j]+Data[i][j]; + Else AData[i][j]=max (data[i-1][j],data[i-1][j-1]) +data[i][j];//results from top down at } - } -tmp=0; - for(intj=0; j<n;j++)//find the maximum value - if(tmp<data[n-1][j]) -tmp=data[n-1][j]; incout<<tmp<<Endl; - } to return 0; +}
View Code
The first DP problem, the idea of this problem is, from top to bottom to save up
Master of this question http://blog.csdn.net/lalor/article/details/6954923
Digital Triangle 17 First-in DP