http://acm.pku.edu.cn/JudgeOnline/problem?id=1163 #include <iostream> using namespace std; int Trangle[100][100]; int DpTrangle[100][100]; int main() { int row; cin>>row; //输入三角形 for( int i = 0 ; i < row ; ++i ) for( int j = 0; j <= i; ++j ) { cin>>Trangle[i][j]; } //动态规划 计算三角形的最大和 for( int i = 0 ; i < row ; ++i ) for( int j = 0; j <= i; ++j ) { if( i == 0 && j == 0 ) { DpTrangle[i][j] = Trangle[i][j]; } else { DpTrangle[i][j] = ( DpTrangle[i-1][j-1] + Trangle[i][j] ) > ( DpTrangle[i-1][j] + Trangle[i][j] ) ? (DpTrangle[i-1][j-1] + Trangle[i][j]):(DpTrangle[i-1][j] + Trangle[i][j]); } } int max = 0; for( int i = 0; i < row; ++i ) { max = ( DpTrangle[row - 1][i] > max ? DpTrangle[row -1][i]: max ); } cout<<max<<endl; return 0; }