蓝桥杯 数字三角形 Java代码

来源:互联网 发布:51单片机at指令 编辑:程序博客网 时间:2024/05/22 11:51
import java.util.Scanner;


public class DigitalTriangle {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[][] array = new int[n][];
        for (int i = 0; i < n; i++) {
            array[i] = new int[i + 1];
            for (int j = 0; j < i + 1; j++) {
                array[i][j] = input.nextInt();
            }
        }
        int sum = 0;
        for(int row=n-2;row>=0;row--){
            for(int colum=0;colum<row+1;colum++){
                array[row][colum]=Math.max(array[row][colum]+array[row+1][colum], array[row][colum]+array[row+1][colum+1]);
            }
        }
        System.out.println(array[0][0]);
    }
}
0 0
原创粉丝点击