#include <iostream>
#include<time.h>
using namespace std;
int main()
{
cout<<"请输入矩阵的行和列数";
int m,n;
cin>>m>>n;
clock_t start,finish;
start=clock();
int **a=new int *[m];
for (int i=0;i<m;i++)
{
a[i]=new int [n];
}
int col1=0,col2=n-1;
int row1=0,row2=m-1;
int situ=0;
int num=1;
while(num<=m*n)
{
switch(situ)
{
case 0:for (int i=col1;i<=col2;i++)
{
a[row1][i]=num;
num++;
}
row1++;
situ=1;
break;
case 1:for (int i=row1;i<=row2;i++)
{
a[i][col2]=num;
num++;
}
col2--;
situ=2;
break;
case 2:for(int i=col2;i>=col1;i--)
{
a[row2][i]=num;
num++;
}
row2--;
situ=3;
break;
case 3:for (int i=row2;i>=row1;i--)
{
a[i][col1]=num;
num++;
}
col1++;
situ=0;
break;
default:break;
}
}
for (int i=0;i<=m-1;i++)
{
for (int j=0;j<=n-1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
finish=clock();
cout<<"\n"<<(finish-start)/CLK_TCK<<endl;
system("pause");
}