代码如下:
#include "stdio.h"
#define N 50
int a[N][N][3];
int n;
void operate(){
int i,j;
for (i = n-1; i >=1 ; i--)
for(j=1;j<=i;j++)
if (a[i+1][j][2]>a[i+1][j+1][2])
a[i][j][2]+=a[i+1][j][2];
else
{
a[i][j][2]+=a[i+1][j][2];
a[i][j][0]=1;
}
}
void path(){
int i,k;
printf("%d",a[1][1][1]);
k=1;
for(i=2;i<=n;i++){
k+=a[i][k][0];
printf("-->%d",a[i][k][1]);
}
}
void main(){
int i,j;
printf("请输入塔层:");
scanf("%d",&n);
printf("请输入数据:\n");
for ( i = 1; i <= n; i++)
for(j=1;j<=i;j++)
{
scanf("%d",&a[i][j][1]);
a[i][j][2]=a[i][j][1];
a[i][j][0]=0;
}
operate();
printf("路径最大和为:%d\n",a[i][j][2]);
path();
}