杨辉三角的实现

杨辉三角的代码实现以及打印
效果图:
在这里插入图片描述

public class YangHui {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test(7);
	}
	public static void test(int n) {
		int[][] array = new int[n][];
		for (int i = 0; i < array.length; i++) {
			array[i] = new int[i + 1];
			for (int j = 0; j < array[i].length; j++) {
				if (j == 0 || j == i) {
					array[i][j] = 1;
				} else {
					array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
				}
			}
		}
		for (int[] arr : array) {
			for (int a : arr) {
				System.out.print(a + "\t");
			}
			System.out.println();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43400357/article/details/85321662