zoukankan      html  css  js  c++  java
  • 蓝桥杯 算法训练 数字三角形

    问题描述
      (图3.1-1)示出了一个数字三角形。 请编一个程序计算从顶至底的某处的一条路
      径,使该路径所经过的数字的总和最大。
      ●每一步可沿左斜线向下或右斜线向下走;
      ●1<三角形行数≤100;
      ●三角形中的数字为整数0,1,…99;


      .
      (图3.1-1)
    输入格式
      文件中首先读到的是三角形的行数。

      接下来描述整个三角形
    输出格式
      最大总和(整数)
    样例输入
    5
    7
    3 8
    8 1 0
    2 7 4 4
    4 5 2 6 5
    样例输出
    30
     
    import java.util.*;
    public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int [][] array = new int[110][110];
    int [][] max = new int[110][110];
    for(int i=1;i<=num;i++)
    for(int j=1;j<=i;j++)
    array[i][j]=sc.nextInt();
    for(int k=1;k<=num;k++)
    max[num][k] = array[num][k];
    for(int i=num-1;i>=1;i--)
    for(int j=1;j<=i; j++)
    if(max[i+1][j]>max[i+1][j+1])
    max[i][j] = array[i][j] + max[i+1][j];
    else
    max[i][j] = array[i][j] + max[i+1][j+1];
    System.out.println(max[1][1]);
    }
    }



    此问题的关键在于动态规划
  • 相关阅读:
    转C#与CSV
    转Log4Net配置
    转sp_addlinkedserver实现远程数据库链接
    HTML DOM
    JavaScript学习
    2016年的个人计划-xiangjiejie
    sass转换为css
    返回上一页显示上次操作后的界面
    angular ng-bind-html 对src路径失效 解决方案
    angular json转义html
  • 原文地址:https://www.cnblogs.com/1iHu4D0n9/p/8510382.html
Copyright © 2011-2022 走看看