• 欢迎光临~

# 【图形】晕

n。正方形的边长

边长为 n 的数字回形方阵。

测试输入 期待的输出 时间限制 内存限制 额外进程

1. 3↵

1.   1  2  3↵
2.   8  9  4↵
3.   7  6  5↵
1秒 64M 0

1. 4↵

1.   1  2  3  4↵
2.  12 13 14  5↵
3.  11 16 15  6↵
4.  10  9  8  7↵
1秒 64M 0

1. 5↵

1.   1  2  3  4  5↵
2.  16 17 18 19  6↵
3.  15 24 25 20  7↵
4.  14 23 22 21  8↵
5.  13 12 11 10  9↵
1秒 64M 0

``````#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void outer_frame(int start, int end, int a, int num_sq[][10]);
int main(void)
{
int a = 1;
int* p_a = &a;
int x = 0, y = 0;
int n;
scanf("%d", &n);
int num_sq[10][10] = {0};
int start = 0, end = n - 1;

outer_frame(start, end, a,num_sq);

int counter = 0;
for (x = 0; x < n; x++) {
for (y = 0; y < n; y++) {
printf("%3d", num_sq[x][y]);
counter++;
if ((counter % n == 0) && (counter!=n*n))
putchar('n');
}
}
return 0;
}
void outer_frame(int start, int end, int a,int num_sq[][10]) {
int x = 0, y = 0;
for (x = start, y = start; y < end+1; y++) {
num_sq[x][y] = a++;
//printf("num_sq[%d][%d] = %3dn", x, y, num_sq[x][y]);
}
for (y = end, x = start+1; x < end+1; x++) {
num_sq[x][y] = a++;
//printf("num_sq[%d][%d] = %3dn", x, y, num_sq[x][y]);
}
for (x = end, y = end-1; y > start; y--) {
num_sq[x][y] = a++;
//printf("num_sq[%d][%d] = %3dn", x, y, num_sq[x][y]);
}
for (x = end, y = start; x > start; x--) {
num_sq[x][y] = a++;
//printf("num_sq[%d][%d] = %3dn", x, y, num_sq[x][y]);
}
start += 1; end -= 1;
if (end > 0)
outer_frame(start, end, a, num_sq);
}``````