循环语句 + 入门判定
2023-07-31 08:57:33
发布于:浙江
56阅读
0回复
0点赞
#include <iostream>
#include <iomanip>
using namespace std;
const int N=3005;
int a[N][N];
int n,i,j;
int main() {
cin>>n;
int x=0, y=-1,num=1;
while(num <= n*n) {
while(y+1 < n && a[x][y+1] == 0){
++y;
a[x][y] = num;
++num;
}
while(x+1 < n && a[x+1][y] == 0){
++x;
a[x][y] = num;
++num;
}
while(y-1 >= 0 && a[x][y-1] == 0){
--y;
a[x][y] = num;
++num;
}
while(x-1 >= 0 && a[x-1][y] == 0){
--x;
a[x][y] = num;
++num;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
这里空空如也
有帮助,赞一个