杭电oj —— 2032

package com.demo2;

import java.util.Scanner;

/*
 * 还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
 */
public class HDU_oj2032 {
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		while (sn.hasNext()) {
			int n = sn.nextInt();
			int[][] arr = new int[n][n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j <= i; j++) {
					if (j == 0 || j == i) {
						arr[i][j] = 1;
					} else {
						arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
					}
				}
			}
			for (int i = 0; i < n; i++) {
				for (int j = 0; j <= i; j++) {
					if(i == j)
						System.out.println(arr[i][j]);
					else
						System.out.print(arr[i][j] + " ");	
				}
			}
			System.out.println();
			
		}
		sn.close();
	}
}

 

你可能感兴趣的:(杭电oj)