文章
105
粉丝
69
获赞
117
访问
56.8k
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int n, cnt = 1;
int res[N][N];
int main()
{
cin >> n;
int top = 1,bottom = n,left = 1,right = n;
int cnt = 1;
while(1)
{
for(int i = top; i <= bottom; i ++) res[i][left] = cnt ++;
left ++;
if(left > right) break;
for(int i = left; i <= right; i ++) res[bottom][i] = cnt ++;
bottom --;
if(top > bottom) break;
for(int i = bottom; i >= top; i --) res[i][right] = cnt ++;
right --;
if(right < left) break;
for(int i = right; i >= left; i --) res[top][i] = cnt ++;
top ++;
if(bottom < top) break;
}
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= n; j ++)
printf("%-4d", res[i][j]);
cout << endl;
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发