56分求解

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

wb913292851 @ 2022-11-02 20:16:03

#include <iostream>
using namespace std;

int main()
{//输入
    int a; cin >> a;
    int** b = new int*[a];
    for (int i = 0; i < a; i++)
    {
        b[i] = new int[a];
        for (int j = 0; j <= i; j++)
        {
            cin >> b[i][j];
        }
    }
 // 从下到上计算
    for (int i = a - 2; i >= 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (b[i+1][j] <=b[i+1][j+1] )
            {
                b[i][j] += b[i + 1][j + 1];
            }
            else
            {
                b[i][j] += b[i + 1][j];
            }

        }
    }
    b[0][0] += max(b[1][0], b[1][1]);//补充第一行的结果
    cout << b[0][0];
  //求问问题在哪
  //https://www.luogu.com.cn/record/92579749
    return 0;
}

by mediocre_ @ 2022-12-13 20:25:15

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1009;
int r, a[N][N], f[N][N];
int main() {
    scanf("%d", &r);
    for (int i = 1; i <= r; ++i)
        for (int j = 1; j <= i; ++j)
            scanf("%d", &a[i][j]);
    for (int i = 1; i <= r; ++i)
        f[r][i] = a[r][i];
    for (int i = r - 1; i >= 1; --i)
        for (int j = 1; j <= i; ++j)
            f[i][j] = max(f[i + 1][j], f[i + 1][j + 1]) + a[i][j];
    printf("%d", f[1][1]);
    return 0;
}

by mediocre_ @ 2022-12-13 20:25:54

@wb913292851 没有看懂你的思路,你可以讲一遍你的思路吗?


|