文章
34
粉丝
0
获赞
354
访问
7.2k
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
scanf("%d",&n);
int num=1;
int a[20][20]={{0}};
int top=0,bottom=n-1,left=0,right=n-1;
while(num<=n*n){
for(int i=top;i<=bottom;i++)
a[i][left]=num++;
left++;
for(int i=left;i<=right;i++)
a[bottom][i]=num++;
bottom--;
for(int i=bottom;i>=top;i--)
a[i][right]=num++;
right--;
for(int i=right;i>=left;i--)
a[top][i]=num++;
top++;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%-3d ",a[i][j]);
}
printf("\n");
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发