#include <iostream>
#include <string.h>
using namespace std;
#define MAX 100
int A[MAX][MAX];
void printMatrix(int n)
{
memset(A,0,sizeof(int)*MAX*MAX);
int sum=0;
A[0][0]=++sum;
int i=0,j=0;
while(sum<n*n)
{
while (j<n-1 && A[i][j+1]==0)
{
A[i][++j]=++sum;
}
while (i<n-1 && A[i+1][j]==0)
{
A[++i][j]=++sum;
}
while (j>0 && A[i][j-1]==0)
{
A[i][--j]=++sum;
}
while(i>0 && A[i-1][j]==0)
{
A[--i][j]=++sum;
}
}
for (int i=0;i<n;++i)
{
for (int j=0;j<n;++j)
{
printf("%3d ",A[i][j]);
}
printf("\n");
}
}
int main()
{
printMatrix(4);
}
转载于:https://www.cnblogs.com/wuhayaoshenmeai/p/3361877.html