#include
#include
int N,M;
int main(int argc, char *argv[]) {
while(scanf("%d",&M)==1)
{
while(scanf("%d",&N) != EOF)
{
int A[N][N];
int B[N+1][N+1];
int i,j;
for(i = 0; i= 0; i--)
for(j = 0; j<=i; j++)
{
if(B[i+1][j]>=B[i+1][j+1])
B[i][j] = A[i][j] + B[i+1][j];
else
B[i][j] = A[i][j] +B[i+1][j+1];
}
printf("%d\n",B[0][0]);
}
}
return 0;
}
解题思路:上一层的最大值取决于下层的最大值;用数组A[N]记录每一个节点;B[N+1]记录过程中的最长路径;注意bottom up!