蓝桥杯_FJ的字符串

问题描述
  FJ在沙盘上写了这样一些字符串:
  A1 = “A”
  A2 = “ABA”
  A3 = “ABACABA”
  A4 = “ABACABADABACABA”
  … …
  你能找出其中的规律并写所有的数列AN吗?
输入格式
  仅有一个数:N ≤ 26。
输出格式
  请输出相应的字符串AN,以一个换行符结束。输出中不得含有多余的空格或换行、回车符。
样例输入
3
样例输出
ABACABA

有两个方法,递归和动态规划

//递归
#include<iostream>
#include<string>
using namespace std;

string lm(int n)
{
	if(n == 1)
	{
		return "A";
	}		
	char mid = 'A'+n-1;
	return lm(n-1) + mid + lm(n-1);
}

int main()
{
	int n;
	cin >> n;
	cout << lm(n);
    return 0;
}
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int n;
	cin >> n;
	string Fj[26];
	string tmp;
	Fj[0] = "A";
	for(int i = 1; i < 26; i++)
	{
		tmp = 'A'+i;
		Fj[i] = Fj[i-1]+tmp+Fj[i-1];
	}
	cout << Fj[n-1];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41282102/article/details/88372659