HDU 2084 数塔+NYOJ 18 The Triangle

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084

http://acm.nyist.net/JudgeOnline/problem.php?pid=18

这是最简单的动态规划题了,思路就是从后往前找,so easy。。。。。。,直接水过

代码:

#include<stdio.h>
#include<string.h>
int a[101][101];
int main()
{
    int s,n,i,j;
    scanf("%d",&s);
    memset(a,0,sizeof(a));
    while(s--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=i;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(i=n-1;i>=1;i--)//**从后往前找**//
        {
            for(j=1;j<=i;j++)
            {
                if(a[i+1][j]>a[i+1][j+1])
                {
                    a[i][j]=a[i][j]+a[i+1][j];
                }
                else
                {
                    a[i][j]=a[i][j]+a[i+1][j+1];
                }
            }
        }
        printf("%d\n",a[1][1]);
    }
    return 0;
}


你可能感兴趣的:(HDU 2084 数塔+NYOJ 18 The Triangle)