求解

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

wangyilin @ 2019-12-15 11:36:50

#include <iostream>
using namespace std;
int f[101][101];
int a[101][101];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=i;j++)
            cin>>a[i][j];
    f[1][1]=a[1][1];
    for(int i=2;i<=n;i++) 
        for(int j=1;j<=i;j++)
            f[i][j] = max(f[i-1][j], f[i-1][j-1]) + a[i][j];

    int ans=-1;
    for(int j=1;j<=n;j++)
        ans = max(ans , f[n][j]);
    cout<<ans<<endl; 
    return 0;
}

55分, RE 4个点,我太难了!

dalao帮我看看,谢谢!


by jijidawang @ 2020-01-06 21:49:04


by jijidawang @ 2020-01-06 21:50:33

#include<iostream>
using namespace std;
int i,j,maxn,n,a[1001][1001],f[1001][1001];
int main()
{
    cin>>n;
    for (i=1;i<=n;i++)
        for (j=1;j<=i;j++)
            cin>>a[i][j];
    f[1][1]=a[1][1];
    for (i=1;i<=n;i++)   //这里i=1,不是i=2
        for (j=1;j<=i;j++)
            f[i][j]=a[i][j]+max(f[i-1][j-1],f[i-1][j]);
    maxn=-1;
    for (i=1;i<=n;i++)
        if (f[n][i]>maxn) maxn=f[n][i];
    cout<<maxn;
    return 0;
}

by jijidawang @ 2020-01-06 21:50:43

@wangyilin


by wangyilin @ 2020-01-07 16:49:22

谢谢大佬,蒟蒻感激不尽


|