package com.lf.trianglenumber;

public class Test {
    public static void main(String[] args) {
        // 打印的行数
        int count = 10;
        // 创建二维数组
        int[][] triangleNum = new int[count][];
        // 遍历,创建一维数组并赋值
        for (int i = 0; i < triangleNum.length; i++) {
            //创建一维数组
            triangleNum[i] = new int[i+1];
            // 遍历,给一维数组赋值
            for (int j = 0; j < triangleNum[i].length; j++) {
                //判断是否是首位和末位,如果是就赋值1,不是就用上一行相邻的数相加
                if (j==0 || j==triangleNum[i].length-1) {
                    triangleNum[i][j] = 1;
                }else {
                    triangleNum[i][j] = triangleNum[i-1][j]+triangleNum[i-1][j-1];
                }
                System.out.print(""+triangleNum[i][j]+"\t");
            }
            System.out.println("");
        }
        
        
    }
}

 

相关文章:

  • 2022-12-23
  • 2022-01-21
  • 2021-11-16
  • 2021-08-14
  • 2021-04-20
  • 2021-05-11
  • 2021-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2021-11-04
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
相关资源
相似解决方案