螺旋矩阵

 #include<iostream>

using namespace std;
 
int main()
{
int a[100][100];
int b[2];
int n,k,t;
int x,y;
cout<<"请输入一个数字:"<<endl;
cin>>n;
x=1;
t=1;
k=n;
b[0]=0;
b[1]=1;
while(x<=n*n)
{
for(y=1;y<=2*k-1;y++)
{
b[y/(k+1)]=b[y/(k+1)]+t;
a[b[0]][b[1]]=x;
x++;
}
k=k-1;
t=-t;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}

你可能感兴趣的:(职场,include,休闲)