```
#include<bits/stdc++.h>
using namespace std;
int a[15][15];
int main()
{
int n,k=1,x=1,y=0;;
cin>>n;
while (k<=n*n)
{
while(y<n&&!a[x][y+1])a[x][++y]=k++;
while(x<n&&!a[x+1][y])a[++x][y]=k++;
while(y>1&&!a[x][y-1])a[x][--y]=k++;
while(x>1&&!a[x-1][y])a[--x][y]=k++;
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)printf("%3d",a[i][j]);
printf("\n");
}
return 0;
}
```
试试看,我这方法跟你的挺像的
by pow_na @ 2022-12-26 10:30:21
@[wch666](/user/778382)
by pow_na @ 2022-12-26 10:30:52
~~打表一遍A~~
by ZQIN_P @ 2022-12-26 10:39:50
@[wch666](/user/778382) 改好力
```
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
cin>>n;
int b=n*n,c=1,i=1,j=0,a[100][100]; //改动 1 :i 的初始值
while(c<=b)
{
while(j+1<=n&&a[i][j+1]==0)
a[i][++j]=c++;
while(i+1<=n&&a[i+1][j]==0)
a[++i][j]=c++;
while(j-1>=1&&a[i][j-1]==0)
a[i][--j]=c++;
while(i-1>=1&&a[i-1][j]==0)
a[--i][j]=c++;
}
for(int i=1; i<=n; i++) //改动 2 :输出方式
{
for(int j=1; j<=n; j++)
{
cout << setw(4) << a[i][j] ; //setw(x) 作用是使元素右对齐占 x 格输出
}
cout<<endl;
}
return 0;
}
```
by ISTP @ 2022-12-26 10:40:31
@[planet_over_for_ever](/user/638459) 你又来水谷力
by ISTP @ 2022-12-26 10:40:59
@[QiMi](/user/528540) 当然罗,数学课太没意思了
by ZQIN_P @ 2022-12-26 10:41:31
送一段打表代码
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int a;
scanf("%d",&a);
if(a==1){
printf(" 1");
}if(a==2){
printf(" 1 2\n");
printf(" 4 3");
}if(a==3){
printf(" 1 2 3\n");
printf(" 8 9 4\n");
printf(" 7 6 5");
}if(a==4){
printf(" 1 2 3 4\n");
printf(" 12 13 14 5\n");
printf(" 11 16 15 6\n");
printf(" 10 9 8 7");
}if(a==5){
printf(" 1 2 3 4 5\n");
printf(" 16 17 18 19 6\n");
printf(" 15 24 25 20 7\n");
printf(" 14 23 22 21 8\n");
printf(" 13 12 11 10 9");
}if(a==6){
printf(" 1 2 3 4 5 6\n");
printf(" 20 21 22 23 24 7\n");
printf(" 19 32 33 34 25 8\n");
printf(" 18 31 36 35 26 9\n");
printf(" 17 30 29 28 27 10\n");
printf(" 16 15 14 13 12 11");
}if(a==7){
printf(" 1 2 3 4 5 6 7\n");
printf(" 24 25 26 27 28 29 8\n");
printf(" 23 40 41 42 43 30 9\n");
printf(" 22 39 48 49 44 31 10\n");
printf(" 21 38 47 46 45 32 11\n");
printf(" 20 37 36 35 34 33 12\n");
printf(" 19 18 17 16 15 14 13");
}if(a==8){
printf(" 1 2 3 4 5 6 7 8\n");
printf(" 28 29 30 31 32 33 34 9\n");
printf(" 27 48 49 50 51 52 35 10\n");
printf(" 26 47 60 61 62 53 36 11\n");
printf(" 25 46 59 64 63 54 37 12\n");
printf(" 24 45 58 57 56 55 38 13\n");
printf(" 23 44 43 42 41 40 39 14\n");
printf(" 22 21 20 19 18 17 16 15");
}if(a==9){
printf(" 1 2 3 4 5 6 7 8 9\n");
printf(" 32 33 34 35 36 37 38 39 10\n");
printf(" 31 56 57 58 59 60 61 40 11\n");
printf(" 30 55 72 73 74 75 62 41 12\n");
printf(" 29 54 71 80 81 76 63 42 13\n");
printf(" 28 53 70 79 78 77 64 43 14\n");
printf(" 27 52 69 68 67 66 65 44 15\n");
printf(" 26 51 50 49 48 47 46 45 16\n");
printf(" 25 24 23 22 21 20 19 18 17");
}
return 0;
}
```
用来本地测试,勿复制粘贴
by ZQIN_P @ 2022-12-26 10:44:46
可以了thankyou了
by wch666 @ 2022-12-27 10:11:52