/**
* 打印杨辉三角形
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
*/
package ma;
public class YangHui {
public static void main(String[] args) {
final int MAX = 20;
int[][] number = new int[MAX][MAX];
for(int i=1; i<=MAX-1; i++) {
for (int j=1; j<=i; j++) {
if ((j==1) || (j==i)) {
number[i][j] = 1;
} else {
number[i][j] = number[i-1][j-1]+number[i-1][j];
}
System.out.print(number[i][j] + " ");
}
System.out.print("/n");
}
}
}
Java实例23 - 打印杨辉三角形
最新推荐文章于 2024-08-22 16:23:38 发布