1 solutions

  • 0
    @ 2025-11-5 15:01:24

    C :

    #include <stdio.h>
    #include <string.h>
    const int maxN = 11;
    int main(void)
    {
    	int num[maxN][maxN];
    	int N;
    	while(scanf("%d", &N) == 1)
    	{
    		int x = N - 1, y = 0, sum = 1, tar = N * N;
    		memset(num, 0, sizeof(num));
    		num[y][x] = sum;
    		while(sum != tar)
    		{
    			while(y + 1 < N && !num[y + 1][x])
    				num[++y][x] = ++sum;
    			while(x - 1 >= 0 && !num[y][x - 1])
    				num[y][--x] = ++sum;
    			while(y - 1 >= 0 && !num[y - 1][x])
    				num[--y][x] = ++sum;
    			while(x + 1 < N && !num[y][x + 1])
    				num[y][++x] = ++sum;
    			
    		}
    		for(int i = 0; i < N; i++)
    		{
    			for(int j = 0; j < N; j++)
    			{
    				printf("%3d", num[i][j]);
    			}
    			printf("\n");
    		}
    		printf("\n");
    	}
    	return 0;
    }
    

    C++ :

    #include <iostream>   
    #include <iomanip>   
       
    using namespace std;   
       
    int circle(int n,int row,int col,int first);//回环数的递归调用函数   
    int arr[100][100];//保存输入n*n回环数字   
    int n;//全局n   
    int count;   
    int main()   
    {   
        while(cin>>n)   
        {   
    		count=0;  
            circle(n,0,0,1);//回环初始   
            for(int i=0;i<n;i++)//输出回环   
            {   
                for(int j=0;j<n;j++)   
                    cout<<setw(3)<<arr[i][j];//控制每次输出宽度   
                cout<<endl;   
            }
    		cout<<endl;
        }  
    	return 0;
    }   
     
    int circle(int n,int row,int col,int first)//回环数的递归函数,每次完成一圈赋值   
    {   
        int row2 = row;   
        int col2 = col;   
        for(;row<n-1;row++)//列向下输出n-1   
            arr[row][n-col-1+count] = first++;   
        for(;col<n-1;col++)//横向左输出n-1   
            arr[row][n-col-1+count] = first++;   
        for(;row>col2;row--)//列向上输出n-1   
            arr[row][n-col-1+count] = first++;   
        for(;col>row2;col--)//横向右输出n-1   
            arr[row][n-col-1+count]  = first++;          
        if(n-row>1)//没有完成,调用递归完成下一圈赋值   
        {   	
    			count++;
                circle(n-1,row+1,col+1,first);   
    		
        }   
        else  
        {   
            if(n-row==1)//n是奇数时,要单独处理中心数   
            {   
                arr[row][col]=first;   
                return 0;   
            }   
       
        }  
    
    }
    

    Java :

    
    
    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		final int UP = 0;
    		final int DOWN = 1;
    		final int LEFT = 2;
    		final int RIGHT = 3;
    		while(scanner.hasNextInt()) {
    			int n = scanner.nextInt();
    			int[][] arr = new int[n][n];
    			int direction  = DOWN;
    			int x = 0;
    			int y = n - 1;
    			int count = 1;
    			while(true) {
    				if(arr[x][y] != 0) {
    					break;
    				}
    				
    				if(direction == DOWN ) {
    					arr[x++][y] = count++;
    					
    					if(x == n || arr[x][y] != 0) {
    						x--;
    						y--;
    						if(y == -1 ) {
    							y = n - 1;
    						}
    						direction = LEFT;
    					}
    					
    				} else if(direction == LEFT) {
    					arr[x][y--] = count++;
    					
    					if(y == -1 || arr[x][y] != 0) {
    						x--;
    						y++;
    						direction = UP;
    					}
    					
    				} else if(direction == UP ) {
    					arr[x--][y] = count++;
    					
    					if(x == -1 || arr[x][y] != 0) {
    						x++;
    						y++;
    						direction = RIGHT;
    					}
    					
    				} else if(direction == RIGHT) {
    					arr[x][y++] = count++;
    					
    					if(arr[x][y] != 0) {
    						x++;
    						y--;
    						direction = DOWN;
    					}
    				}
    			}
    			
    			for(int i = 0 ; i < n ; i++ ) {
    				for(int j = 0 ; j < n ; j++) {
    					System.out.printf("%3d", arr[i][j]);
    				} 
    				System.out.println();
    			}
    			System.out.println();
    		}
    		scanner.close();
    	}
    
    }
    
    
    • 1

    Information

    ID
    16336
    Time
    1000ms
    Memory
    64MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By