Java面试宝典_基础编程练习题_杨辉三角形
程序分析:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
代码/** * @author :xy.hero@qq.com * @date
题目
题目:打印出杨辉三角形(要求打印出10行如下图)
程序分析:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
代码
/** * @author :xy.hero@qq.com * @date :Created in 2019-07-12 13:13 * @description:www.jiagou1216.com * @modified By: * @version: $ */ public class Test { public static void main(String[] args) { int[][] n = new int[10][21]; n[0][10] = 1; for (int i = 1; i < 10; i++) for (int j = 10 - i; j < 10 + i + 1; j++) n[i][j] = n[i - 1][j - 1] + n[i - 1][j + 1]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 21; j++) { if (n[i][j] == 0) System.out.print(" "); else { if (n[i][j] < 10) System.out.print(" " + n[i][j]);//空格为了美观需要 else if (n[i][j] < 100) System.out.print(" " + n[i][j]); else System.out.print(n[i][j]); } } System.out.println(); } } }
输出
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1