题解 | #杨辉三角#
杨辉三角
http://www.nowcoder.com/practice/e671c6a913d448318a49be87850adbcc
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i ; j++) {
if (i == j || j == 0) {
arr[i][j] = 1;
} else {
// 上一行的数和上一行前面的数相加
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
}
}
// 输出结果
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.printf("%5d", arr[i][j]);
}
System.out.println();
}
}
}