ECNU || 梵高先生

思路

杨辉三角形

代码

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] a = new int[n+1][n+1];
		a[1][1] = 1;
		for (int i = 1; i <= n; i++) {
			a[i][1] = 1;
			a[i][i] = 1;
			for (int j = 2; j <= i-1 ; j++) {
				a[i][j] = a[i-1][j-1]+a[i-1][j];
			}
		}
		for (int i = 1; i < a.length; i++) {
			for (int j = 1; j <= i; j++) {
				if(j==i)
					System.out.print(a[i][j]);
				else
					System.out.print(a[i][j]+" ");
			}
			System.out.println();
		}
	}
}

你可能感兴趣的:(刷题)