Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
334022 | 黄浙峰老师 | 【基础】螺旋方阵 | C++ | 通过 | 100 | 2 MS | 252 KB | 764 | 2025-07-17 11:04:58 |
#include<bits/stdc++.h> using namespace std; int a[20][20]; //start:起始点坐标 //len:赋值长度 //x:起始值 void f(int start, int len, int x){ //向右 if(len>0){ for(int j=start; j<=start+len-1; j++){ a[start][j]=x; x++; } //向下 for(int i=start+1; i<=start+len-1; i++){ a[i][start+len-1]=x; x++; } //向左 for(int j=start+len-2; j>=start; j--){ a[start+len-1][j]=x; x++; } //向上 for(int i=start+len-2; i>=start+1; i--){ a[i][start]=x; x++; } start++; len=len-2; f(start,len,x); } } int main(){ int n; cin>>n; f(1,n,1); for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ cout<<setw(3)<<a[i][j]; } cout<<endl; } return 0; }