数字三角形问题-dp

在这里插入图片描述
思想:自底向上,dp[0][0]记录路径数字和的最大值。

方式1:键盘输入,控制台输出。

import java.util.Scanner;

public class NumberTriangle {
    /**
     * 自底向上的方式,使用triangle[0][0]存储最大路径值
     * @param n 输入的数字三角形行数
     * @param triangle 数字三角形
     * @return
     */
    public static int dp(int n, int [][] triangle){
        for(int i=n-2; i>=0; i--){
            for(int j=0; j<=i; j++){
                if(triangle[i+1][j] >= triangle[i+1][j+1]){
                    triangle[i][j] += triangle[i+1][j] ;
                }else{
                    triangle[i][j] += triangle[i+1][j+1] ;
                }
            }
        }
        return triangle[0][0] ;
    }
    public static void main(String[] args){
        Scanner input = new Scanner(System.in) ;
        int n = input.nextInt() ;
        int [][] triangle = new int [n][n] ;
        for(int i=0; i<n; i++){
            for(int j=0; j<=i; j++){
                triangle[i][j] = input.nextInt() ;
            }
        }
        System.out.println(dp(n, triangle)) ;
    }
}

方式2:文件流输入输出。

import java.io.*;

public class NumberTriangle1 {
    public static String line ;
    public static int i=0, j=0, n, temp;
    static int [][] triangle = new int [1000][1000] ;
    public static int dp(int n, int [][] triangle){
        for(int i=n-2; i>=0; i--){
            for(int j=0; j<=i; j++){
                if(triangle[i+1][j] >= triangle[i+1][j+1]){
                    triangle[i][j] += triangle[i+1][j] ;
                }else{
                    triangle[i][j] += triangle[i+1][j+1] ;
                }
            }
        }
        return triangle[0][0] ;
    }
    public static void main(String[] args) {
        try(FileInputStream fileInputStream = new FileInputStream("D:\\study\\input.txt");
            DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(fileInputStream))){
            line = dataInputStream.readLine() ;
            n = Integer.parseInt(line) ;
            temp = n ;
            while(n > 0){
                line = dataInputStream.readLine() ;
                String [] s = line.split("[ ]") ;
                for(int k=0; k<s.length; k++) {
                    triangle[i][j] =Integer.parseInt(s[k]) ;
                    if(j<=i)
                    j ++ ;
                }
                i ++ ;
                j = 0 ;
                n -- ;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try(FileOutputStream fileOutputStream = new FileOutputStream("D:\\study\\output.txt");
            DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(fileOutputStream))) {
            dataOutputStream.writeBytes(String.valueOf(dp(temp, triangle)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值