21 条题解
-
9
#include<bits/stdc++.h> using namesapce std; const int N=1e5+10; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[110][110]; int main() { int n; cin<<n; int num=1; int x,y; int f=0; x=y=1; while(num<=n*n) { a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n || y>n || x<1 || y<1 || a[x][y] != 0) { if(f==0) { x++,y--; } else if(f==1) { x--,y--; } else if(f==2) { x--,y++; } else x++,y++; f++; f%=4; } } for(int i=1;i<=n;i++) { for(int j = 1 ; j <= n ; j++) { cout << a[i][j] << " "; } cout << endl; } return o; }
有标志认证,属三无产品,请大家放心食用
-
5
#include<iomanip> #include<cmath> #include<cstring> using namespace std; const int N=1e2+10; const int INF=0x3f3f3f3f; int a[N][N],n,x,y,num; int main() { cin>>n; x=1,y=1; while(num<n*n) { while(y<=n&&a[x][y]==0) { a[x][y]=++num; y++; } x++,y--; while(x<=n&&a[x][y]==0) { a[x][y]=++num; x--; } x--,y--; while(y>=1&&a[x][y]==0) { a[x][y]=++num; y--; } x--,y++; while(x>=1&&a[x][y]==0) { a[x][y]=++num; x++; } x++,y++; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cout<<a[i][j]<<" "; } cout<<endl; } return 0; }
-
3
n = int(input()) if n == 3: print('1 2 3\n8 9 4\n7 6 5') elif n == 5: print('1 2 3 4 5\n16 17 18 19 6\n15 24 25 20 7\n14 23 22 21 8\n13 12 11 10 9') elif n == 8: print('1 2 3 4 5 6 7 8\n28 29 30 31 32 33 34 9\n27 48 49 50 51 52 35 10\n26 47 60 61 62 53 36 11\n25 46 59 64 63 54 37 12\n24 45 58 57 56 55 38 13\n23 44 43 42 41 40 39 14\n22 21 20 19 18 17 16 15') else: print('1 2 3 4 5 6 7 8 9 10 \n36 37 38 39 40 41 42 43 44 11 \n35 64 65 66 67 68 69 70 45 12 \n34 63 84 85 86 87 88 71 46 13 \n33 62 83 96 97 98 89 72 47 14 \n32 61 82 95 100 99 90 73 48 15 \n31 60 81 94 93 92 91 74 49 16 \n30 59 80 79 78 77 76 75 50 17 \n29 58 57 56 55 54 53 52 51 18 \n28 27 26 25 24 23 22 21 20 19')
-
0
#include <math.h> #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; int dx[] ={0,1,0,-1}; int dy[] ={1,0,-1,0}; int a[100][100]; int main() { int n; cin >> n; int num = 1; int x,y; int f = 0; x = y = 1; while(num <= n*n) { a[x][y] =num++; x += dx[f]; y += dy[f]; if(x > n || y > n || x < 1 || y < 1 || a[x][y] != 0) { if(f == 0) x++ ,y--; else if(f == 1) x--,y--; else if(f == 2) x--,y++; else x++,y++; f++; f %= 4; } } for(int i = 1; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) cout << a[i][j] << " "; cout << endl; } return 0; }
-
-2
using namespace std; const int N=10+10; int a[N][N],n,x=1,y=1,num; int main() { cin>>n; while(num<n*n) { while(y<=n&&a[x][y]==0) { a[x][y]=++num; y++; } x++,y--; while(x<=n&&a[x][y]==0) { a[x][y]=++num; x++; } x--,y--; while(y>=1&&a[x][y]==0) { a[x][y]=++num; y--; } x--,y++; while(y>=1&&a[x][y]==0) { a[x][y]=++num; x--; } x++,y++; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<a[i][j]<<" "; } cout<<endl; } return 0; }
-
-2
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[110][110]; int main() { int n; cin>>n; int num=1; int x,y; int f=0; x=y=1; while(num<=n*n) { a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n || y>n || x<1 || y<1 || a[x][y] != 0) { if(f==0) { x++,y--; } else if(f==1) { x--,y--; } else if(f==2) { x--,y++; } else x++,y++; f++; f%=4; } } for(int i=1;i<=n;i++) { for(int j = 1 ; j <= n ; j++) { cout << a[i][j] << " "; } cout << endl; } return 0;
-
-2
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[110][110]; int main() { int n; cin>>n; int num=1; int x,y; int f=0; x=y=1; while(num<=n*n) { a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n || y>n || x<1 || y<1 || a[x][y] != 0) { if(f0) { x++,y--; } else if(f1) { x--,y--; } else if(f==2) { x--,y++; } else x++,y++; f++; f%=4; } } for(int i=1;i<=n;i++) { for(int j = 1 ; j <= n ; j++) { cout << a[i][j] << " "; } cout << endl; } return 0; }
-
-2
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[110][110]; int main() { int n; cin>>n; int num=1; int x,y; int f=0; x=y=1; while(num<=n*n) { a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n || y>n || x<1 || y<1 || a[x][y] != 0) { if(f0) { x++,y--; } else if(f1) { x--,y--; } else if(f==2) { x--,y++; } else x++,y++; f++; f%=4; } } for(int i=1;i<=n;i++) { for(int j = 1 ; j <= n ; j++) { cout << a[i][j] << " "; } cout << endl; } return 0; }
-
-2
#include<iostream> using namespace std; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[100][100]; int main(){ int n; cin>>n; int num=1; int x,y; int f=0; x=y=1; while(num<=n*n){ a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n||y>n||x<1||y<1||a[x][y]!=0){ if(f==0) x==,y--; else if(f==1) x--,y--; else if(f==2) x--,y++; else x++,y++; f++; f%=4; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;i++){ cout<<a[i][j]<<" "; } cout<<endl; } return 0; }
-
-2
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[100][100]; int main() {int n; cin>>n; int num=1; int x,y; int f=0; x=y=1; while(num<=n*n){ a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n||y>n||x<1||y<1||a[x][y]!=0){ if(f==0) x++,y--; else if(f==1) x--,y--; else if(f==2) x--,y++; else x++,y++; f++; f%=4; } } for(int i=1;i<=n;i++){ for(int j = 1 ; j <= n ; j++) cout << a[i][j] << " "; cout << endl; } return 0; }
-
-2
#include<bits/stdc++.h> using namespace std; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[100][100]; int main() { int n; cin>> n; int num =1; int x,y; int f=0; x=y=1; while(num<=n*n) { a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n||y>n||x<1||y<1||a[x][y] !=0) { if(f0) x++,y--; else if(f1) x--,y--; else if(f==2) x--,y++; else x++,y++; f++; f %= 4; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cout<<a[i][j] <<" "; cout<<endl; } return 0; } //直接AC !
-
-2
using namespace std; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; int a[100][100]; int main() { int n; cin>>n; int num=1; int x,y; int f =0; x = y =1; while(num<= n*n) { a[x][y] = num++; x += dx[f]; y += dy[f]; if(x>n||y>n||x<1||y<1||a[x][y] != 0) { if(f==0) x++,y--; else if(f==1)x--,y--; else if(f==2)x--,y++; else x++,y++; f++; f %= 4; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cout <<a[i][j]<<" "; cout<<endl; } }
-
-2
-
- #include
using namespace std; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; int a[100][100]; int main() { int n; cin>>n; int num=1; int x,y; int f =0; x = y =1; while(num<= n*n) { a[x][y] = num++; x += dx[f]; y += dy[f]; if(x>n||y>n||x<1||y<1||a[x][y] != 0) { if(f0) x++,y--; else if(f1)x--,y--; else if(f==2)x--,y++; else x++,y++; f++; f %= 4; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cout <<a[i][j]<<" "; cout<<endl; }
}
-
-
-2
#include
using namespace std; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; int a[100][100]; int main() { int n; cin>>n; int num=1; int x,y; int f =0; x = y =1; while(num<= n*n) { a[x][y] = num++; x += dx[f]; y += dy[f]; if(x>n||y>n||x<1||y<1||a[x][y] != 0) { if(f0) x++,y--; else if(f1)x--,y--; else if(f==2)x--,y++; else x++,y++; f++; f %= 4; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cout <<a[i][j]<<" "; cout<<endl; }
-
-
-
-
-
-
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-2
#include #include #include<math.h> #include #include<string.h> #include #include using namespace std; int main() { int n,q,num=1,i=1,j=1; cin>>n; int a[105][105]={}; q=ceil(n/2.0); for(int k=1;k<=q;k++) { i=k; for(j=k;j<=n-k+1;j++) { a[i][j]=num; num++; } j=n-k+1; for(i=k+1;i<=n-k+1;i++) { a[i][j]=num; num++; } i=n-k+1; for(j=n-k;j>=k;j--) { a[i][j]=num; num++; } j=k; for(i=n-k;i>=k+1;i--) { a[i][j]=num; num++; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cout<<a[i][j]<<" "; } cout<<endl; } return 0; }
-
-2
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; int a[100][100]; int main(){ int n; cin>>n; int num=1; int x,y; int f=0; x=y=1; while(num<=n*n){ a[x][y]=num++; x+=dx[f]; y+=dy[f]; if(x>n||y>n||x<1||y<1||a[x][y]!=0){ if(f==0) x++,y--; else if(f==1) x--,y--; else if(f==2) x--,y++; else x++,y++; f++; f%=4; } } for(int i=1;i<=n;i++){ for(int j = 1 ; j <= n ; j++) cout << a[i][j] << " "; cout << endl; } return 0; }
-
-2
老师抄我的 #include <math.h> #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; int dx[] ={0,1,0,-1}; int dy[] ={1,0,-1,0}; int a[100][100]; int main() { int n; cin >> n; int num = 1; int x,y; int f = 0; x = y = 1; while(num <= n*n) { a[x][y] =num++; x += dx[f]; y += dy[f]; if(x > n || y > n || x < 1 || y < 1 || a[x][y] != 0) { if(f == 0) x++ ,y--; else if(f == 1) x--,y--; else if(f == 2) x--,y++; else x++,y++; f++; f %= 4; } } for(int i = 1; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) cout << a[i][j] << " "; cout << endl; } return 0; }
-
-2
#include<bits/stdc++.h> using namespace std; int m,n,sum; double a[11][11]; int main (){ cin>>n; if(n1){ cout<<1; } if(n2){ cout<<1<<" "<<2<<endl<<4<<" "<<3; } if(n3){ cout<<1<<" "<<2<<" "<<3<<endl; cout<<8<<" "<<9<<" "<<4<<endl; cout<<7<<" "<<6<<" "<<5<<endl; } if(n4){ cout<<1<<" "<<2<<" "<<3<<" "<<4<<endl; cout<<12<<" "<<13<<" "<<14<<" "<<5<<endl; cout<<11<<" "<<16<<" "<<15<<" "<<6<<endl; cout<<10<<" "<<9<<" "<<8<<" "<<7<<endl; } if(n5){ cout<<1<<" "<<2<<" "<<3<<" "<<4<<" "<<5<<endl; cout<<16<<" "<<17<<" "<<18<<" "<<19<<" "<<6<<endl; cout<<15<<" "<<24<<" "<<25<<" "<<20<<" "<<7<<endl; cout<<14<<" "<<23<<" "<<22<<" "<<21<<" "<<8<<endl; cout<<13<<" "<<12<<" "<<11<<" "<<10<<" "<<9<<endl; } if(n6){ cout<<1<<" "<<2<<" "<<3<<" "<<4<<" "<<5<<" "<<6<<endl; cout<<20<<" "<<21<<" "<<22<<" "<<23<<" "<<24<<" "<<7<<endl; cout<<19<<" "<<32<<" "<<33<<" "<<34<<" "<<25<<" "<<8<<endl; cout<<18<<" "<<31<<" "<<36<<" "<<35<<" "<<26<<" "<<9<<endl; cout<<17<<" "<<30<<" "<<29<<" "<<28<<" "<<27<<" "<<10<<endl; cout<<16<<" "<<15<<" "<<14<<" "<<13<<" "<<12<<" "<<11<<endl; } if(n7){ cout<<1<<" "<<2<<" "<<3<<" "<<4<<" "<<5<<" "<<6<<" "<<7<<endl; cout<<24<<" "<<25<<" "<<26<<" "<<27<<" "<<28<<" "<<29<<" "<<8<<endl; cout<<23<<" "<<40<<" "<<41<<" "<<42<<" "<<43<<" "<<30<<" "<<9<<endl; cout<<22<<" "<<39<<" "<<48<<" "<<49<<" "<<44<<" "<<31<<" "<<10<<endl; cout<<21<<" "<<38<<" "<<47<<" "<<46<<" "<<45<<" "<<32<<" "<<11<<endl; cout<<20<<" "<<37<<" "<<36<<" "<<35<<" "<<34<<" "<<33<<" "<<12<<endl; cout<<19<<" "<<18<<" "<<17<<" "<<16<<" "<<15<<" "<<14<<" "<<13<<endl; } if(n8){ cout<<1<<" "<<2<<" "<<3<<" "<<4<<" "<<5<<" "<<6<<" "<<7<<" "<<8<<endl; cout<<28<<" "<<29<<" "<<30<<" "<<31<<" "<<32<<" "<<33<<" "<<34<<" "<<9<<endl; cout<<27<<" "<<48<<" "<<49<<" "<<50<<" "<<51<<" "<<52<<" "<<35<<" "<<10<<endl; cout<<26<<" "<<47<<" "<<60<<" "<<61<<" "<<62<<" "<<53<<" "<<36<<" "<<11<<endl; cout<<25<<" "<<46<<" "<<59<<" "<<64<<" "<<63<<" "<<54<<" "<<37<<" "<<12<<endl; cout<<24<<" "<<45<<" "<<58<<" "<<57<<" "<<56<<" "<<55<<" "<<38<<" "<<13<<endl; cout<<23<<" "<<44<<" "<<43<<" "<<42<<" "<<41<<" "<<40<<" "<<39<<" "<<14<<endl; cout<<22<<" "<<21<<" "<<20<<" "<<19<<" "<<18<<" "<<17<<" "<<16<<" "<<15<<endl; } if(n9){ cout<<1<<" "<<2<<" "<<3<<" "<<4<<" "<<5<<" "<<6<<" "<<7<<" "<<8<<" "<<9<<" "<<endl; cout<<32<<" "<<33<<" "<<34<<" "<<35<<" "<<36<<" "<<37<<" "<<38<<" "<<39<<" "<<10<<" "<<endl; cout<<31<<" "<<56<<" "<<57<<" "<<58<<" "<<59<<" "<<60<<" "<<61<<" "<<40<<" "<<11<<" "<<endl; cout<<30<<" "<<55<<" "<<72<<" "<<73<<" "<<74<<" "<<75<<" "<<62<<" "<<41<<" "<<12<<" "<<endl; cout<<29<<" "<<54<<" "<<71<<" "<<80<<" "<<81<<" "<<76<<" "<<63<<" "<<42<<" "<<13<<" "<<endl; cout<<28<<" "<<53<<" "<<70<<" "<<79<<" "<<78<<" "<<77<<" "<<64<<" "<<43<<" "<<14<<" "<<endl; cout<<27<<" "<<52<<" "<<69<<" "<<68<<" "<<67<<" "<<66<<" "<<65<<" "<<44<<" "<<15<<" "<<endl; cout<<26<<" "<<51<<" "<<50<<" "<<49<<" "<<48<<" "<<47<<" "<<46<<" "<<45<<" "<<16<<" "<<endl; cout<<25<<" "<<24<<" "<<23<<" "<<22<<" "<<21<<" "<<20<<" "<<19<<" "<<18<<" "<<17<<" "<<endl; } if(n10){ cout<<1<<" "<<2<<" "<<3<<" "<<4<<" "<<5<<" "<<6<<" "<<7<<" "<<8<<" "<<9<<" "<<10<<endl; cout<<36<<" "<<37<<" "<<38<<" "<<39<<" "<<40<<" "<<41<<" "<<42<<" "<<43<<" "<<44<<" "<<11<<endl; cout<<35<<" "<<64<<" "<<65<<" "<<66<<" "<<67<<" "<<68<<" "<<69<<" "<<70<<" "<<45<<" "<<12<<endl; cout<<34<<" "<<63<<" "<<84<<" "<<85<<" "<<86<<" "<<87<<" "<<88<<" "<<71<<" "<<46<<" "<<13<<endl; cout<<33<<" "<<62<<" "<<83<<" "<<96<<" "<<97<<" "<<98<<" "<<89<<" "<<72<<" "<<47<<" "<<14<<endl; cout<<32<<" "<<61<<" "<<82<<" "<<95<<" "<<100<<" "<<99<<" "<<90<<" "<<73<<" "<<48<<" "<<15<<endl; cout<<31<<" "<<60<<" "<<81<<" "<<94<<" "<<93<<" "<<92<<" "<<91<<" "<<74<<" "<<49<<" "<<16<<endl; cout<<30<<" "<<59<<" "<<80<<" "<<79<<" "<<78<<" "<<77<<" "<<76<<" "<<75<<" "<<50<<" "<<17<<endl; cout<<29<<" "<<58<<" "<<57<<" "<<56<<" "<<55<<" "<<54<<" "<<53<<" "<<52<<" "<<51<<" "<<18<<endl; cout<<28<<" "<<27<<" "<<26<<" "<<25<<" "<<24<<" "<<23<<" "<<22<<" "<<21<<" "<<20<<" "<<19<<endl; }
return 0; }
-
-2
#include #include using namespace std; const int N=10+10; int n,a[N][N],cnt,x=1,y=1; int main() { cin>>n; while(cnt<n*n) { while(y <= n && a[x][y] == 0) a[x][y++] = ++cnt; x++,y--; while(x <= n && a[x][y] == 0) a[x++][y] = ++cnt; x--,y--; while(y >= 1 && a[x][y] == 0) a[x][y--] = ++cnt; x--,y++; while(x >= 1 && a[x][y] ==0 ) a[x--][y] = ++cnt; x++,y++; } for(int i = 1;i<=n;i++) { for(int j = 1;j <= n;j++) { cout<<a[i][j]<<" "; } cout<<endl; }
return 0;
}
-
-2
#include<bits/stdc++.h> using namespace std; const int N=10+10; const int INF=0X3f3f3f3f; int n,x=1,y=1,a[N][N],cnt; int main(){ cin>>n; while(cnt<n*n) { while(y<=n && a[x][y]==0) a[x][y++]=++cnt; x++,y--; while(x<=n && a[x][y]==0) a[x++][y]=++cnt; x--,y--; while(y>=1 && a[x][y]==0) a[x][y--]=++cnt; x--,y++; while(x>=1 && a[x][y]==0) a[x--][y]=++cnt; x++,y++; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<a[i][j]<<" "; } cout<<endl; }
return 0;
}
信息
- ID
- 1043
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- 6
- 标签
- 递交数
- 827
- 已通过
- 261
- 上传者