题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] arr = new int[n][n]; arr[0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0, d = j + 2; j < n - i; j++) { if (i == 0 && j == 0) { arr[i][j] = 1; } else if (i > 0 && j == 0) { arr[i][j] = arr[i - 1][0] + i; } else { arr[i][j] = arr[i][j - 1] + i + d++; } System.out.print(arr[i][j] + " "); } System.out.println(); } } }
#华为笔试#