Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
95470 | hby | 螺旋矩阵 | C++ | Accepted | 1 MS | 284 KB | 508 | 2024-10-27 19:30:49 |
#include<bits/stdc++.h> #include<cstdio> #define maxn 30 int a[maxn][maxn]; using namespace std; int main(){ int n,x,y,tot; cin>>n; memset(a,0,sizeof(a)); x=0;y=0; tot=a[0][0]=1; while(tot<n*n){ while(y+1<n&&!a[x][y+1])a[x][++y]=++tot; while(x-1>=0&&!a[x-1][y])a[--x][y]=++tot; while(y-1>=0&&!a[x][y-1])a[x][--y]=++tot; while(x+1<n&&!a[x+1][y])a[++x][y]=++tot; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ printf("%4d",a[i][j]); } cout<<endl; } return 0;}