已經(jīng)告訴你了,這是個DP的題目,你能AC嗎? Input輸入數(shù)據(jù)首先包括一個整數(shù)C,表示測試實例的個數(shù),每個測試實例的第一行是一個整數(shù)N(1 <= N <= 100),表示數(shù)塔的高度,接下來用N行數(shù)字表示數(shù)塔,其中第i行有個i個整數(shù),且所有的整數(shù)均在區(qū)間[0,99]內(nèi)。 Output對于每個測試實例,輸出可能得到的最大和,每個實例的輸出占一行。 Sample Input1573 88 1 0 2 7 4 44 5 2 6 5 Sample Output30 Source2006/1/15 ACM程序設計期末考試#include<iostream>#include<string>#include<math.h>#include<string.h>#include<stdio.h>#include<algorithm>using namespace std;int main(){ int num; cin>>num; while(num--) { int a[101][101],i,j,h; cin>>h; for(i=1;i<=h;i++) for(j=1;j<=i;j++) cin>>a[i][j]; for(i=h-1;i>=0;i--) for(j=1;j<=i;j++) a[i][j]+=max(a[i+1][j],a[i+1][j+1]);//每次取最大值 cout<<a[1][1]<<endl; } return 0;}
新聞熱點
疑難解答