zoukankan      html  css  js  c++  java
  • 数塔问题

    考虑在下面被显示的数字金字塔。 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大。 每一步可以走到左下方的点也可以到达右下方的点。

            7 
          3   8 
        8   1   0 
      2   7   4   4 
    4   5   2   6   5 

    在上面的样例中,从7 到 3 到 8 到 7 到 5 的路径产生了最大和:30

    Input

    第一个行包含 R(1<= R<=1000) ,表示行的数目。 后面每行为这个数字金字塔特定行包含的整数。 所有的被供应的整数是非负的且不大于100。

    Output

    单独的一行包含那个可能得到的最大的和。

    Sample Input

    5
    7
    3 8
    8 1 0
    2 7 4 4
    4 5 2 6 5

    Sample Output

    30
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    const int MAX=1e6+1;
    int flag[MAX]={0};
    int main()
    {
        ll n,i,j;
        ll a[1010][1010]={0};
        cin>>n;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=i;j++)
                cin>>a[i][j];
        }
        for(i=n-1;i>=1;i--)
        {
            for(j=1;j<=i;j++)
            {
                a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
            }
        }
        cout<<a[1][1]<<endl;
        return 0;
    
    }
    
  • 相关阅读:
    E: 无法获得锁 /var/lib/dpkg/lock-frontend
    Ubuntu 18.04 更换apt源
    ubuntu18.04
    小a与“204”------数列、排序
    星际穿越
    合唱团---DP
    分苹果---暴力
    地牢逃脱----DFS搜索最优解
    下厨房---map/字符串查询
    hdu 2654 Be a hero
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702703.html
Copyright © 2011-2022 走看看