java-杨辉三角(代码)
import java.util.Arrays;
import java.util.Scanner;
// 杨辉三角打印(输入行数,输出杨辉三角)
public class Work15_3_14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
for (int [] date :yang_Hui_Triangle(input)
) {
System.out.println(Arrays.toString(date));
}
print(yang_Hui_Triangle(input));
}
public static int[][] yang_Hui_Triangle(int num){
int [][] list = new int[num][num];
list [0][0] = 1;
for (int i = 0; i < num; i++) {
list [i][0] = 1;
for (int j = 1; j < i; j++) {
list[i][j] = list[i-1][j-1]+list[i-1][j];
}
list [i][i] = 1;
}
return list;
}
public static void print(int [][] list){
// 控制每一行
for (int i = 0; i < list.length; i++) {
// 前面补空格
for (int j = 0; j < list.length-1-i; j++) {
System.out.print(" ");
}
// 控制每一列
for (int j = 0; j <= i; j++) {
System.out.print(" ");
System.out.printf("%-3d",list[i][j]);
}
System.out.println();
}
}
}