杨辉三角

 1 import java.util.Scanner;
 2 
 3 public class Yanghui {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         System.out.println("请输入行列数");
 7         int num = sc.nextInt();
 8         int[][] a = new int[num][num];
 9         for (int i = 0; i < a.length; i++) {
10             for (int j = 0; j < i + 1; j++) {
11                 // 输出所有的1,其他的数再想办法
12                 a[i][0] = 1;
13                 a[i][i] = 1;
14                 if (i >= 2 && j >= 1) {
15                     // 从第三行起,中间的不等于1的数的值其实是上一个一位数组的两个相邻元素的和
16                     a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
17                 }
18                 System.out.print(a[i][j] + "	");
19             }
20             System.out.println();
21         }
22     }
23 }
24 请输入行列数
25 4
26 1    
27 1    1    
28 1    2    1    
29 1    3    3    1    
public class MyYanghui {
    public static void main(String[] args) {
        int num = 8;
        int[][] arr =new int[num][num];
        for(int i =0;i<arr.length;i++) {
            for(int j=0;j<=i;j++) {
                if(j==0||i==j) {
                    arr[i][j]=1;
                System.out.print(arr[i][j]+"	");
                }else {
                    arr[i][j] = arr[i-1][j-1]+arr[i-1][j];
                    System.out.print(arr[i][j]+"	");
                }
            }
            System.out.println();
        }
    }
}
1    
1    1    
1    2    1    
1    3    3    1    
1    4    6    4    1    
1    5    10    10    5    1    
1    6    15    20    15    6    1    
1    7    21    35    35    21    7    1
原文地址:https://www.cnblogs.com/19322li/p/10599461.html