2 条题解

  • 5
    @ 2026-6-25 19:18:06
    #include<iostream>
    #include<cstdio>
    
    using namespace std;
    
    int a[10][10];
    
    int main(){
    	int n,now = 2,i = 1,j = 1;
    	cin>>n;
    	a[1][1] = 1;
    	while(now<=n*n){
    		//向右 a[i][j]  i不变 j++
    		while(j + 1<=n&&a[i][j+1] == 0){
    			j++;
    			a[i][j] = now;
    			now++;
    		}
    		//向下 i++,j不变
    		while(i + 1 <= n&&a[i+1][j] == 0){
    			i++;
    			a[i][j] = now;
    			now++;
    		}
    		//向左 i不变,j--
    		while(j - 1 >= 1 &&a[i][j-1] == 0){
    			j--;
    			a[i][j] = now;
    			now++;
    		}
    		//向上 i-- ,j不变 
    		while(i - 1 >= 1&&a[i-1][j] == 0){
    			i--;
    			a[i][j] = now;
    			now++;
    		}
    	}
    	for(int x = 1;x<=n;x++){
    		for(int y = 1;y<=n;y++){
    //			cout<<setw(3)<<a[x][y];
    			printf("%3d",a[x][y]);
    		}
    		cout<<endl;
    	}
    	
    	return 0;
    } 
    
    • 0
      @ 2026-7-8 11:29:34

      #include #include #include #include

      using namespace std; int main(){ int h[100][100],n,k=1,a,b,c,d,e=1;//右//下 ←上 cin>>n; int A=n,B=n,C=n,D=n,E=2,F=1; a=1; b=n; c=n; d=1; e=1; if(n==1){ cout<<setw(3)<<1; }else{ for(int i=1;i<=n/2+1;i++){ for(int j=a;j<=A;j++){ h[a][j]=e; e++; } a++; A--; for(int j=E;j<=B;j++){ h[j][b]=e; e++; } b--; B--; E++; for(int j=C-1;j>=F;j--){ h[c][j]=e; e++; } F++; c--; C--; for(int j=D-1;j>=a;j--){ h[j][d]=e; e++; } d++; D--; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<setw(3)<<h[i][j]; } cout<<endl; } } return 0; }

      • 1

      信息

      ID
      80
      提交时间
      1000ms
      内存
      256MiB
      难度
      1
      标签
      递交数
      108
      已通过
      36
      上传者