题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
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]; int p = 0; for (int i = 1; i <= N; i++) { int j = i, k = 1; while (j >= 1 && k <= i) { a[j][k] = ++p; j--; k++; } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N - i + 1; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } }