编辑代码

#include <iostream>
#include <iomanip>
using namespace std;

const int N = 50+10;
int n,a[N][N];

int main()
{
	
		cin>>n; 
		if(n==0) return 0;
		
		int x=0,y=0,res=a[x][y]=1;
		//起点
		
		while(res < n*n )
		{
			while(!a[x][y+1] && y+1<n) a[x][++y] = ++res;
			
			while(!a[x+1][y] && x+1<n) a[++x][y] = ++res;
			
			while(!a[x][y-1] && y-1>=0) a[x][--y] = ++res;
			
			while(!a[x-1][y] && x-1>=0) a[--x][y] = ++res;
		 }
		 
		 for(x=0;x<n;x++)
		 {
		   for(y=0;y<n;y++)
		    {
		     	cout<<setw(3)<<a[x][y]<<' ';
			 }
			cout<<endl;

		    
	    }
	
	return 0;
 }