#include <stdio.h>
int main() {
int matrix[100][100];
int m;
int n;
int total;
int count = 0;
int base = 0;
scanf("%d %d", &m, &n);
total = m * n;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
}
}
while (1) {
for (int i = base; i < n - 1 - base; ++i) {
printf("%d", matrix[base][i]);
count++;
if (count < total) {
printf(" ");
} else {
break;
}
}
if (count >= total) break;
for (int i = base; i < m - 1 - base; ++i) {
printf("%d", matrix[i][n - 1 - base]);
count++;
if (count < total) {
printf(" ");
} else {
break;
}
}
if (count >= total) break;
for (int i = n - 1 - base; i > base; --i) {
printf("%d", matrix[m - 1 - base][i]);
count++;
if (count < total) {
printf(" ");
} else {
break;
}
}
if (count >= total) break;
for (int i = m - 1 - base; i > base; --i) {
printf("%d", matrix[i][base]);
count++;
if (count < total) {
printf(" ");
} else {
break;
}
}
if (count >= total) break;
base++;
}
return 0;
}
矩阵螺旋输出
最新推荐文章于 2024-09-13 16:27:15 发布