zoukankan      html  css  js  c++  java
  • POJ 2760: 数字三角形

    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) 
        {
            int[][] triNumbers = new int[101][101];//0-100内
            int[][] triMaxs = new int[101][101]; 
            
            Scanner in = new Scanner(System.in);
            int nrow = in.nextInt();//行数
            for(int i=0;i<nrow;i++)//
            {
                for(int j=0;j<=i;j++)//行内
                {
                    triNumbers[i][j] = in.nextInt();
                }
            }
            in.close();
            for(int i = nrow-1;i>=0;i--)
            {
                for(int j=i;j>=0;j--)//行内
                {
                    if(nrow==i)
                        triMaxs[i][j] = triNumbers[i][j];//最后一行
                    else 
                        triMaxs[i][j] =  Math.max(triMaxs[i+1][j], triMaxs[i+1][j+1])+triNumbers[i][j];
                }
            }    
            System.out.println(triMaxs[0][0]);
        }
    }

    题目:

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述
    7
    3 8
    8 1 0
    2 7 4 4
    4 5 2 6 5

    (图1)

    图1给出了一个数字三角形。从三角形的顶部到底部有很多条不同的路径。对于每条路径,把路径上面的数加起来可以得到一个和,你的任务就是找到最大的和。

    注意:路径上的每一步只能从一个数走到下一层上和它最近的左边的那个数或者右边的那个数。
    输入
    输入的是一行是一个整数N (1 < N <= 100),给出三角形的行数。下面的N行给出数字三角形。数字三角形上的数的范围都在0和100之间。
    输出
    输出最大的和。
    样例输入
    5
    7
    3 8
    8 1 0 
    2 7 4 4
    4 5 2 6 5
    样例输出
    30
    来源
    翻译自 IOI 1994 的试题
    一些些总结:
    This problem is adopted in Programming Guide. It adapts dynamic  planning to solving the problem, which takes less space and time than the measure of recursion.
    Although the algorithm was described clearly, I still got "WA" when first pubmitted my src.
    After I compared the standard answer with mine , I found that the standard one adds 10 to the max boundary of the array.
    Well, after checking, I find in the circulation,i or j can be 101,so the max boundary must be no less than 101.
    The result always goes back on a careless man...
  • 相关阅读:
    jdk源码阅读笔记之java集合框架(四)(LinkedList)
    jdk源码阅读笔记之java集合框架(二)(ArrayList)
    jdk源码阅读笔记之java集合框架(三)(modCount)
    java文件拷贝的一点思考
    mac(10.11.5 )安装pt-query-digest所遇问题总结
    关于springboot启动所需所有jar包详解
    volatile的一点理解
    java 虚拟机自动内存管理
    虚拟机运行时数据区划分
    笔记本连上wifi(WiFi完全没问题)却无法上网
  • 原文地址:https://www.cnblogs.com/danscarlett/p/5656289.html
Copyright © 2011-2022 走看看