1 条题解

  • 0
    @ 2023-6-11 12:22:23

    C++ :

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cmath>
    using namespace std;
    const int maxn = 200;
    char seq[maxn] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    char print_seq[maxn][maxn];
    int n;
    
    void f(int idx, int len, int x, int y){
    
        if(idx == -1){
            return ;
        }       
        for(int dx = -(len); dx <= (len); dx += len ){
            for(int dy = -(len); dy <= (len); dy += len){
                int xx = x + dx;
                int yy = y + dy;
    
                print_seq[xx][yy] = seq[idx];
    
            }
        }
    
        f(idx - 1, len + 1, x, y);
    }
    
    int main(){
    //  fstream cin("a.txt");
        cin>>n; 
        for(int i = 0; i < (2 * n - 1); ++i){
            for(int j = 0; j < (2 * n - 1); ++j){
                print_seq[i][j] = '.';
            }
        }
    
        f(n - 2, 1, n - 1, n - 1);
    
        print_seq[n - 1][n - 1] = seq[n - 1];
    
        for(int i = 0; i < (2 * n - 1); ++i){
            for(int j = 0; j < (2 * n - 1); ++j){
                cout<<print_seq[i][j];
            }
            cout<<endl;
        }   
    
        return 0;
    }
    

    Java :

    import java.util.Scanner;
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		
    		//接收用户输入的“边长”
    		n = sc.nextInt();
    		//如果边长为1,直接输出得了
    		if(n == 1){
    			System.out.println("A");
    			return;
    		}
    		int b = n*2-1;
    		char arr[][] = new char[b][b];
    		for(int i = 0; i < b; i++){
    			for(int j = 0; j < b; j++){
    				arr[i][j] = '.';
    			}
    		}
    		
    		f(arr, b);
    		
    		//打印结果图像
    		for(int i = 0; i < b; i++){
    			for(int j = 0; j < b; j++){
    				System.out.print(arr[i][j]);
    			}
    			System.out.println();
    		}
    	}
    	
    	static int n;
    	static void f(char ch[][], int b){
    		int start = 0;
    		int end = b-1;
    		char w= 'A';
    		int add = b/3+n/3;
    		
    		int cnt = 0;
    		while(true){
    			//由外向内
    			for(int i = start; i <= end; i+=add){
    				ch[i][start] = w;//左
    				ch[start][i] = w;//上
    				ch[i][end] = w;//右
    				ch[end][i] = w;//下
    			}
    			start++;
    			end--;
    			w++;
    			add = add - 1;
    			if(add <= 0)add =1;
    			
    			cnt++;
    			if(cnt == n)break;
    		}
    	}
    }
    
    • 1

    信息

    ID
    2726
    时间
    1000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者