提交记录 #108
提交时间:2024-11-04 22:03:03
语言:c
状态:Unaccepted
编译情况:编译成功
code.c: In function ‘return_matrix’:
code.c:5:22: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
5 | L = 0, r, t, b;
| ^
code.c:5:19: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]
5 | L = 0, r, t, b;
| ^
code.c:5:16: warning: ‘r’ may be used uninitialized in this function [-Wmaybe-uninitialized]
5 | L = 0, r, t, b;
| ^
固定测试点#1:
固定测试点#2:
固定测试点#3:
固定测试点#4:
固定测试点#5:
固定测试点#6:
固定测试点#7:
附加测试点暂不可用
H13【选做•图形】晕
#include <stdio.h>
void return_matrix(int a[][30], int n) {
int top = 0, bottom, left = 0, right,
L = 0, r, t, b;
int count = 1;
bottom = n - 1;
right = n - 1;
if (n == 2) {
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 4;
a[1][1] = 3;
} else if (n == 1) {
a[0][0] = 1;
} else {
while (!(top == bottom && left == right)) {
while (L <= right) {
a[top][L] = count;
count++;
L++;
}
if (count <= n * n) { top++; t = top; }
while (t <= bottom) {
a[t][right] = count;
count++;
t++;
}
if (count <= n * n) { right--; r = right; }
while (r >= left) {
a[bottom][r] = count;
count++;
r--;
}
if (count <= n * n) { bottom--; b = bottom; }
while (b >= top) {
a[b][left] = count;
count++;
b--;
}
if (count <= n * n) { left++; L = left; }
}
if (n % 2 != 0) { a[top][left] = count; }
}
}
int main() {
int a[30][30];
int n = 0;
scanf("%d", &n);
return_matrix(a, n);
// 设定宽度为4以适应最大值100
int width = 4; // 确保每个数字占用4个字符
// 遍历输出
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%*d", width, a[i][j]); // 输出格式化,固定宽度为4
// 仅在当前不是最后一个数字时添加空格
if (j < n - 1) {
printf(" "); // 在数字之间添加空格
}
}
printf("\n"); // 换行
}
return 0;
}