问题描述
FJ在沙盘上写了这样一些字符串:
A1 = “A”
A2 = “ABA”
A3 = “ABACABA”
A4 = “ABACABADABACABA”
… …
你能找出其中的规律并写所有的数列AN吗?
输入格式
仅有一个数:N ≤ 26。
输出格式
请输出相应的字符串AN,以一个换行符结束。输出中不得含有多余的空格或换行、回车符。
样例输入
3
样例输出
ABACABA
`import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 由题意
// 样例输入:3
// 样例输出:ABACABA
// 可知中间的数值是字母排列靠后的
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();//输入判定数字
shuzu(a);
}
public static void shuzu(int a) {//输出的值
String[] s1 = {"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"};
String[] s2 = {"A"};
if (a == 1) {
array(s2);//如果输入1,直接输出A
}else {
for (int i = 2; i <= a ; i++) {
s2 = fan(s2, s1[i-1]);//传入fan的值
}
array(s2);//打印数组
}
}
public static String[] fan(String[] s,String s2) {//返回一个数组为:两边是数组s,中间是数组s2
String[] str = new String[s.length*2+1];
for (int i = 0; i < s.length; i++) {//将s2中所对应的值赋给str数组
str[i] = s[i];
}
str[s.length] = s2;//最中间的值
for (int i = 0; i < s.length; i++) {
str[i+s.length+1] = s[i];//再将后面与前端相同的数值赋给str,以便输出
}
return str;//返回数组
}
public static void array(String[] str) {//打印数组
for (int i = 0; i < str.length; i++) {
if (i%100 == 0) {//对最终答案无实质性影响,只是便于观察
System.out.println();
}
System.out.print(str[i]);
}
}
}
`