题解 | 杨辉三角
杨辉三角
https://www.nowcoder.com/practice/8c6984f3dc664ef0a305c24e1473729e
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int[][] arr = new int[n][n];
arr[0][0]=1;
for(int i=1;i<n;i++){
arr[i][0] = 1;
for(int j=1;j<=i;j++){
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(arr[i][j] != 0){
System.out.printf("%d ",arr[i][j]);
}
}
System.out.println();
}
}
}
}
查看2道真题和解析