杨辉三角
作者:互联网
//编写一个main方法
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.print("输入杨辉三角的层数:");
int nums = myScanner.nextInt();
//创建数组
int[][] arr = new int[nums][];
for (int i = 0; i < nums; i++) {
arr[i] = new int[i + 1];
}
System.out.println(Arrays.toString(arr));
//初始化边框
for (int i = 0; i < nums; i++) {
arr[i][0] = 1;
arr[i][i] = 1;
}
//完善内部
for (int i = 2; i < nums; i++) {
for (int j = 1; j < i; j++) {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
//遍历这个杨辉三角
System.out.println("====杨辉三角如下====");
for (int i = 0; i < nums; i++) {
for (int k = nums - i - 1; k > 0; k--) {
// System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
标签:arr,nums,int,System,++,杨辉三角 来源: https://www.cnblogs.com/chen-zhou1027/p/16522100.html