问题描述
FJ在沙盘上写了这样一些字符串:
A1 = “A”
A2 = “ABA”
A3 = “ABACABA”
A4 = “ABACABADABACABA”
… …
你能找出其中的规律并写所有的数列AN吗?
输入格式
仅有一个数:N ≤ 26。
输出格式
请输出相应的字符串AN,以一个换行符结束。输出中不得含有多余的空格或换行、回车符。
例子
输入
3
输出
ABACABA
#include <stdio.h>
char a[]={'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'};
void f(int n)
{
if(n==1)
{
printf("%c",'A');
}
else
{
f(n-1);
printf("%c",a[n-1]);
f(n-1);
}
}
int main ()
{
int n;
scanf("%d",&n);
f(n);
return 0;
}
运行示例
看别人的C++STL处理方法:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a="A",b;
int n;
cin>>n;
for(int i=2;i<=n;i++){ // n>=2时
b = a;
b.push_back('A'+i-1); // STL,在字符串 b后插入一个字符
a = b+a;
}
cout<<a<<endl;
return 0;
}
也很清晰简单。