题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] matrix = new int[n][n]; matrix[0][0] = 1; for (int i = 1; i < matrix[0].length; i++) { // 第一行的通项公式 matrix[0][i] = (i + 1 + 1) * (i + 1) / 2; } for (int i = 1; i < matrix.length; i++) { for (int j = 0; j < n - i; j++) { matrix[i][j] = matrix[i-1][j] + i + j; } } for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < n - i; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }