package com.hike.javase.work;
public class HelloWork {
public static void main(String[] args) {
// 使用二维数组打印10行杨辉三角
int[][] yangHui = new int[10][];
for(int i = 0; i < yangHui.length; i++) {
yangHui[i] = new int[i + 1];
yangHui[i][0] = 1; //子数组第一个元素
yangHui[i][i] = 1; //子数组第二个元素
for(int j = 1; j < i; j++) {
yangHui[i][j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j];
}
}
for(int[] child : yangHui) {
for(int temp : child) {
System.out.print(temp + " ");
}
System.out.println();
}
}
}
java基础[杨辉三角]
最新推荐文章于 2024-08-10 19:37:00 发布