Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
133554 顾文博 螺旋矩阵 C++ Accepted 2 MS 276 KB 824 2025-10-19 15:18:06

Tests(10/10):


Code:

#include<iostream> // cin\cout\endl #include<cstdio> //scanf()\printf() #include<cstring> // strcpy()\strcat()\strcmp()\strlen()\memset() #include<cmath> //sqrt()\pow()\abs()\ceil()\floor()\max()\min() using namespace std; int main(){ int a[21][21]={0},n,i=0,t=1; cin>>n; while(t<=n*n){ for(int j=i;j<n-i && t<=n*n;j++){ a[i][j]=t++; } for(int j=i+1;j<n-i && t<=n*n;j++){ a[j][n-i-1]=t++; } for(int j=n-i-2;j>=i && t<=n*n;j--){ a[n-i-1][j]=t++; } for(int j=n-i-2;j>i && t<=n*n;j--){ a[j][i]=t++; } i++; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ printf("%4d",a[i][j]); } cout<<endl; } return 0; }