sdut1730 数字三角形问题(dp入门题)

#include<stdio.h>
int a[100][100];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=i;j++)
        scanf("%d",&a[i][j]);
        int maxx  =0;
    for(int i=2;i<=n;i++)
    for(int j=1;j<=i;j++){
        if(j == 1)
        a[i][j] += a[i-1][j];
        else if(j == i) a[i][j] += a[i-1][j-1];
        else if(  (a[i][j] + a[i-1][j]) >  (a[i][j] + a[i-1][j-1]) )
            a[i][j] += a[i-1][j];
        else
             a[i][j] += a[i-1][j-1];
        if(a[i][j]>maxx) maxx = a[i][j];
    }

    printf("%d",maxx);
    return 0;
}

posted on 2017-04-19 19:00  wgwyanfs  阅读(130)  评论(0编辑  收藏  举报

导航