算法训练 s01串
时间限制:1.0s 内存限制:256.0MB
问题描述
s01串初始为"0"
按以下方式变换
0变1,1变01
输入格式
1个整数(0~19)
输出格式
n次变换后s01串
样例输入
3
样例输出
101
数据规模和约定
0~19
解题思路:经观察可以发现,
当n = 0时,返回0
当n = 1时,返回1
else 返回的是字符串fun(n-2) + fun(n-1)的和。
#include <iostream>
using namespace std;
string fun(int n);
int main()
{
int n;
cin >> n;
cout << fun(n) << endl;
return 0;
}
string fun(int n)
{
if(n == 0)
return "0";
else if(n == 1)
return "1";
else
return fun(n-2) + fun(n-1);
}