题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/f228a074c5274619b26be544962375e1
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int n; scanf("%d", &n); int arr[20][20] = { 0 }; int i = 0, j = 0; int count = 0; int sum = 1; while (count <= 2 * n - 2) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i + j == count) { if (count % 2 == 0) { arr[j][i] = sum++; } else { arr[i][j] = sum++; } } } } count++; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }#菜狗的解题#