zoukankan      html  css  js  c++  java
  • 【递归】数字三角形 简单dp

    【递归】数字三角形

     

    题目描述

    对于大多数人来说,“我们是这么的正常,因此也就这么的平庸。”而天才总是与众不同的,所以当邪狼问修罗王:“老大,你蹲在那儿一动不动看了有半个小时了,蚂蚁有那么好看吗?”

    修罗王是这样回答的:“我在思索人生的意义,你看这蚂蚁的面前有无数的道路选择,但它不知道选择哪条路可以到达目标,也不知道哪条路上有更多的食物,更不知道现在选择的道路对它以后的影响……”

    如图所示,有一个层数为n(n≤1000)的数字三角形。现有一只蚂蚁从顶层开始向下走,每走下一级时,可向左下方向或右下方向走。求走到底层后它所经过数字的总和的最大值。

    输入

    第一个整数为n,以下n行为各层的数字。

    输出

    一个整数,即最大值,保证不超过整型的最大范围。

    样例输入

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

    样例输出

    23
    

    提示

    最大值=1+3+6+6+7=23

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
     
    long long int a[1002][1002];
     
    int main()
    {
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= i; j++){
                scanf("%lld",&a[i][j]);
            }
        }
        for(int i = n-1; i >=1; i--){
            for(int j = 1; j <= i; j++){
                a[i][j] += max(a[i+1][j],a[i+1][j+1]);
            }
        }
        printf("%lld
    ",a[1][1]);
        return 0;
    }
    View Code
  • 相关阅读:
    leetcode100
    leetcode237
    leetcode171
    leetcode122
    leetcode387
    2018-8-10-win10-uwp-如何打包Nuget给其他人
    2018-8-10-win10-uwp-如何打包Nuget给其他人
    2019-11-13-如何在国内发布-UWP-应用
    2019-11-13-如何在国内发布-UWP-应用
    2019-2-21-PowerShell-通过-WMI-获取设备厂商
  • 原文地址:https://www.cnblogs.com/cshg/p/5641849.html
Copyright © 2011-2022 走看看