提交记录 #111
提交时间:2024-11-04 22:05:32
语言: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);
// 计算数字输出的宽度,考虑到最大值的位数
int width = (n * n < 10) ? 2 : (n * n < 100) ? 1 : 2; // 选择合适的输出宽度
// 遍历输出,每行前添加空格
for (int i = 0; i < n; i++) {
printf(" "); // 在每一行前添加两个空格
for (int j = 0; j < n - 1; j++) {
printf("%*d ", width, a[i][j]); // 输出格式化,动态宽度
}
printf("%*d\n", width, a[i][n - 1]); // 最后一个数字,避免多余空格
}
return 0;
}