- import java.util.Scanner;
- public class PascalTriangle {public static void main(String[] args) { System.out.print("请输入需要计算的行数: ");
- Scanner scanner = new Scanner(System.in);
- final int num = scanner.nextInt();
- int[][] ary = getPascalTriangle(num); for(int[] item: ary)
- { for(int value: item){ System.out.print((value > 0? value: "") + " ");
- }
- System.out.println();
- }
- }
- private static int[][] getPascalTriangle(final int num)
- { int[][] ary = new int[num][num];
- for(int i = 0; i < ary.length; i++){ ary[i][0] = 1;
- ary[i][i] = 1; }
- for(int i = 1; i < ary.length; i++){ for(int j = 1; j <= i; j++)
- { ary[i][j] = ary[i-1][j-1] + ary[i-1][j]; } } return ary; }}
复制代码
|
|