8 条题解

  • 1
    @ 2023-5-7 18:01:57
    #include<iostream>
    using namespace std;
    const int N=1e5+10;
    int a[110][110],n,x,y,cnt=1;
    int main(){
        cin>>n;
        a[1][1]=1;
        x=2;
        y=1;
        while(cnt<n*n){
            while(x>=1&&y<=n){
                a[x][y]=++cnt;
                x--;
                y++;
            }
            if(x==0&&y<=n){
                x++;
            }else{
                x+=2;
                y--;
            }
            while(y>=1&&x<=n)
            {
                a[x][y]=++cnt;
                x++;
                y--;
            }
            if(y==0&&x<=n){
                y++;
            }else{
                x--;
                y+=2;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
        return 0;
    }
    
  • 0
    @ 2024-11-24 18:27:46
    #include<iostream>
    using namespace std;
    int a[105][105];
    int main(){
    	int n;
    	cin>>n;
    	int x,y;
    	x=y=0;
    	int num=1;
    	int flag=0;
    	while(num<=n*n){
    		a[x][y]=num;
    		if(flag==1){
    			x--;
    			y++;
    			if(y>=n){
    				x+=2;
    				y--;
    				flag=0;
    			}
    			else if(x<0){
    				flag=0;
    				x++;
    			}
    		}
    		else{
    			x++;
    			y--;
    			if(x>=n){
    				x--;
    				y+=2;
    				flag=1;
    			}
    			else if(y<0){
    				y++;
    				flag=1;
    			}
    		}
    		num++;
    	}
    	for(int i=0;i<n;i++){
    		for(int j=0;j<n;j++){
    			cout<<a[i][j]<<" ";
    		}
    		cout<<endl;
    	}
    }
    

    包没错!!!

    • 0
      @ 2024-11-24 18:22:37
      • 0

        #include<iostream> using namespace std; const int N=1e5+10; int a[110][110],n,x,y,cnt=1; int main(){ cin>>n; a[1][1]=1; x=2; y=1; while(cnt<n*n){ while(x>=1&&y<=n){ a[x][y]=++cnt; x--; y++; } if(x0&&y<=n){ x++; }else{ x+=2; y--; } while(y>=1&&x<=n) { a[x][y]=++cnt; x++; y--; } if(y0&&x<=n){ y++; }else{ x--; y+=2; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<a[i][j]<<" "; cout<<endl; } return 0; }

        • 0
          @ 2023-3-21 17:30:23
          #include<iostream>
          using namespace std;
          const int N=1e5+10;
          int a[110][110],n,x,y,cnt=1;
          int main(){
              cin>>n;
              a[1][1]=1;
              x=2;
              y=1;
              while(cnt<n*n){
                  while(x>=1&&y<=n){
                      a[x][y]=++cnt;
                      x--;
                      y++;
                  }
                  if(x==0&&y<=n){
                      x++;
                  }else{
                      x+=2;
                      y--;
                  }
                  while(y>=1&&x<=n)
                  {
                      a[x][y]=++cnt;
                      x++;
                      y--;
                  }
                  if(y==0&&x<=n){
                      y++;
                  }else{
                      x--;
                      y+=2;
                  }
              }
              for(int i=1;i<=n;i++){
                  for(int j=1;j<=n;j++)
                  cout<<a[i][j]<<" ";
                  cout<<endl;
              }
              return 0;
          }
          
          • 0
            @ 2022-11-22 18:17:41

            #include<bits/stdc++.h>

            using namespace std;

            int a[101][101],n,i,j,ans=1;

            int main(){

            cin>>n;
            
            i=1; j=1;
            
            a[i][j]=1;
            
            while(ans<=n*n){
            	if((i==1&&j==n&&n%2==0)||(i==n&&j==1&&n%2!=0)){
            		break;
            	}
            	if(j==1&&i%2!=0){
            		a[i+1][j]=ans+1;
            		ans++;
            		i+=1;
            		while(i!=1){
            			a[i-1][j+1]=ans+1;
            			ans++;
            			i-=1;
            			j+=1;
            		}
            	}
            	else if(i-1<1&&j!=1){
            		a[i][j+1]=ans+1;
            		ans++;
            		j+=1;
            		while(j!=1){
            			a[i+1][j-1]=ans+1;
            			ans++;
            			i+=1;
            			j-=1;
            		}
            	}
            }
            while(ans<=n*n){
            	if(i+1>n){
            		a[i][j+1]=ans+1;
            		ans++;
            		j+=1;
            		while(j!=n){
            			a[i-1][j+1]=ans+1;
            			ans++;
            			i-=1;
            			j+=1;
            		}
            	}
            	else if(j+1>n){
            		a[i+1][j]=ans+1;
            		ans++;
            		i+=1;
            		while(i!=n){
            			a[i+1][j-1]=ans+1;
            			ans++;
            			i+=1;
            			j-=1;
            		}
            	}
            	if(i==n&&j==n){
            		break;
            	}
            }
            for(i=1;i<=n;i++){
            	for(j=1;j<=n;j++){
            		cout<<a[i][j]<<" ";
            	}
            	cout<<endl;
            }
            return 0;
            

            }

            • -1
              @ 2023-3-21 17:27:02

              #include<iostream> using namespace std; const int N = 1e5 + 10;

              int a[110][110],n,x,y,cnt = 1;

              int main(){ cin>>n; a[1][1] = 1; x = 2; y = 1;

              while(cnt < n * n)
              {
              	while(x >= 1 && y <= n){
              		a[x][y] = ++cnt;
              		x--;y++;
              	}
              	if(x == 0 && y <= n){
              		x++;
              	}
              	else
              	{
              		x+=2;
              		y--;
              	}
              	while(y >= 1 && x <= n){
              		a[x][y] = ++cnt;
              		x++;y--; 
              	}
              	
              	if(y==0 && x <= n){
              		y++;
              	}
              	else
              	{
              		x--;y+=2;
              	}
              }
              
              for(int i =1;i<=n;i++)
              {
              	for(int j=1;j<=n;j++)
              	cout<<a[i][j]<<" ";
              	cout<<endl;
              }
              
              return 0;
              

              }

              • -1
                @ 2023-3-21 17:20:06
                #include<iostream>
                using namespace std;
                int a[101][101],n,x,y,cnt=1;
                int main()
                {
                	cin>>n;
                	a[1][1]=1;
                	x=2;y=1;
                	while(cnt<n*n)
                	{
                		//right-up
                		while(x>0&&y<=n)
                		{
                			a[x][y]=++cnt;
                			x--;y++;
                		}
                		if(x==0&&y<=n)x++;
                		else
                		{
                			x+=2;
                			y--;
                		}
                		while(y>0&&x<=n)
                		{
                			a[x][y]=++cnt;
                			y--;x++;
                		}
                		if(y==0&&x<=n)y++;
                		else
                		{
                			x--;
                			y+=2;
                		}
                	}
                	for(int i=1;i<=n;i++)
                	{
                		for(int j=1;j<=n;j++)cout<<a[i][j]<<' ';
                		cout<<endl;
                	}
                	return 0;
                }
                
                • 1

                信息

                ID
                1063
                时间
                1000ms
                内存
                128MiB
                难度
                5
                标签
                递交数
                279
                已通过
                114
                上传者