nylgoj18(动态规划问题)
问题描述:
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int a[104][104];
int main()
{
int i,j,n,t;
memset(a,0,sizeof(a));
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
scanf("%d",&a[i][j]);
for(i=2;i<=n;i++)
for(j=1;j<=i;j++)
{
int max=a[i-1][j-1]>a[i-1][j]?a[i-1][j-1]:a[i-1][j];
a[i][j]=a[i][j]+max;
}
int max=-1;
for(i=1;i<=n;i++)
if(a[n][i]>max)
max=a[n][i];
printf("%d\n",max);
return 0;
}

浙公网安备 33010602011771号