Blue Bridge Cup basic exercises VIP FJ string of questions - JAVA

Problem Description
  FJ in the sand table to write some string like this:
  A1 = "A"
  A2 = "ABA"
  A3 = "ABACABA"
  A4 = "ABACABADABACABA"
  ... ...
  
  you can find out the rules and write them all columns AN it?
  
The input format
  is only a few: N ≤ 26.
  
Output format
  requested output a corresponding string of AN, to a newline character. Output must not contain extra space or line feed, carriage return.
  
Sample input
3

Sample output
ABACABA

PS: seen from the meaning of the questions: A1 = "A"; A2 = A1 + "B" + A1; A3 = A2 + "C" + A2;
! So when n = 1, having the formula: An = An-1 + 'n represented by uppercase alphabetic characters' + An-1;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System. in);
		int n=sc. nextInt(); 
		
		FJStr(n);
	}
	
	static void FJStr(int n){	
		//递归出口
		if(n == 1){
			System.out.print("A");
		}
		else{
			//相当于求和  --> An = An-1 + n表示的字母 + An-1
			FJStr(n-1);
			System.out.print((char)(n+64)); //将此时的行数转换为字母字符
			FJStr(n-1);
		}		
	}

}

Test Results

Published 457 original articles · won praise 646 · views 510 000 +

Guess you like

Origin blog.csdn.net/Czhenya/article/details/104588227