题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int[][] tri = new int[num][num];
//00 10 01 20 11 02 30 21 12 03 40 31 22 13 04
int fz = 1;
for(int i=0;i<num;i++){
int he = i;//观察绿色示例可知,斜线的行列和为一个定值
for(int j=0; j<he+1; j++){
tri[he - j][j] = fz;
fz++;
}
}
for(int i=0; i<num; i++){
for(int j=0; j<num-i; j++){
System.out.print(tri[i][j] + " ");
}
System.out.println();
}
}
}