题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
int a[101][101] = {0};
//memset(a,0,sizeof(a));
int i = 1, cn = n, x = 0, y = 0;
while (cn > 0) {
while (y >= 0 && x < n) {
a[x][y] = i;
x++;
y--;
i++;
}
y++;
int t = x;
x = y;
y = t;
cn--;
}
for (x = 0; x < n; x++) {
for (y = 0; y < n - x; y++)
printf("%d ", a[y][x]);
printf("\n");
}
return 0;
}
查看12道真题和解析