2 条题解
-
112116黄立博 (12116黄立博) LV 10 @ 2022-07-11 19:18:34
#include<bits/stdc++.h> using namespace std; int a[10000],b[100][100],n,a1=0,c=0,s=1,h=1,l=1; int main() { cin>>n;int n1=n,x=1,k,k1; while(s<n*n) { k=x,k1=x; for(k=x,k1=x;k<n1;k++) b[k1][k]=s,s++; //for(int i=1;i<=n;i++)cout<<b[1][i]<<" "; for(k=x,k1=n1;k<n1;k++) b[k][k1]=s,s++; k1=n1,k=n1; for(k=n1,k1=n1;k>x;k--) b[k1][k]=s,s++; for(k=x,k1=n1;k1>x;k1--) b[k1][k]=s,s++; n1--,x++; } if(n%2!=0)b[n/2+1][n/2+1]=s; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cout<<setw(6)<<b[i][j]; cout<<endl; } return 0; } //输出是setw(6)
-
02025-01-13 16:50:52@
#include<iostream> #include<iomanip> using namespace std; int a[101][101]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, num = 1; cin >> n; int p = n*n, q = 0, r = 0; while(num <= p){ for(int ii = 1+r; ii <= n-r; ii++){ if(num > p)goto finish; a[r+1][ii] = num++; //cout << num << '*' << endl; } for(int ii = 2+r; ii < n-r; ii++){ if(num > p)goto finish; a[ii][n-r] = num++; //cout << num << '*' << endl; } for(int ii = n-r; ii >= 1+r; ii--){ if(num > p)goto finish; a[n-r][ii] = num++; //cout << num << '*' << endl; } for(int ii = n-r-1; ii > 1+r; ii--){ if(num > p)goto finish; a[ii][r+1] = num++; //cout << num << '*' << endl; } r++; if(num > p)goto finish;//这行非常神奇,没它就陷入死循环,I don't know why. //cout << r << '#' << endl; } finish: for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++) cout << setw(6) << a[i][j]; cout << endl; } return 0; }
- 1
信息
- ID
- 1101
- 难度
- 6
- 分类
- (无)
- 标签
- 递交数
- 136
- 已通过
- 34
- 通过率
- 25%
- 上传者