利用二维数组输出杨辉三角

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		int i, j, m = 1;
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int[][] Pascal = new int[n][];
		for (i = 0; i < n; i++) {
			Pascal[i] = new int[m];//切记!!这里要进行实例化不然会报空指针的错误
			Pascal[i][0] = 1;
			Pascal[i][m - 1] = 1;
			System.out.print(Pascal[i][0]);
			System.out.print(" ");
			if (i > 1) {
				for (j = 1; j < i; j++) {
					Pascal[i][j] = Pascal[i - 1][j - 1] + Pascal[i - 1][j];
					System.out.print(Pascal[i][j]);
					System.out.print(" ");
				}
			}
			if (i > 0) {
				System.out.print(Pascal[i][m - 1]);
				System.out.println("");
			}
			else
			{
				System.out.println("");
			}
			m++;
		}

	}
}
 

你可能感兴趣的:(java,二维数组,算法,蓝桥杯)