```cpp
while(k<=n*n){
...
}
by xuyi20110411 @ 2024-10-03 13:37:23
循环条件改成 k<n*n
by xuyi20110411 @ 2024-10-03 13:37:59
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[11][11]={0},i,j,n;
int main()
{
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=-1;
}
}
i=1;
j=0;
int k=0;
while(k<n*n){
while(a[i][1+j]==-1)
{
a[i][++j]=++k;
}
while(a[1+i][j]==-1)
{
a[++i][j]=++k;
}
while(a[i][j-1]==-1)
{
a[i][--j]=++k;
}
while(a[i-1][j]==-1)
{
a[--i][j]=++k;}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<setw(3)<<a[i][j];
}
cout<<endl;
}
return 0;
}
by xuyi20110411 @ 2024-10-03 13:39:13
@[xuyi20110411](/user/1036981) 谢谢
by renhan0708 @ 2024-10-03 13:54:56