蓝桥杯:s01串 递归解法

蓝桥杯:s01串 递归解法

问题描述

s01串初始为"0",按以下方式变换:0变1,1变01

输入格式
  1个整数(0~19)
输出格式
  n次变换后s01串
  
样例输入

3

样例输出

101

数据规模和约定 0~19

直接暴力解

限制是1s,只用了31ms

#include <iostream>

using namespace std;

char a[10000];
int len = 1;
int n;

void to_01(int index)
{
	len += 1;
	
	for(int i=len; i>index+1; i--)
	{
		a[i] = a[i-1];
	}
	
	a[index] = '0';
	a[index+1] = '1';
}

void dfs(int x)
{
	if(x <= n)
	{
		for(int i=1; i<=len; i++)
		{
			if(a[i] == '0')
			{
				a[i] = '1';
			}
			else
			{
				to_01(i);
				i++;	// 这里to_01之后,长度++,i也要++ 
			}
		}
		
		dfs(x+1);
	}
}

int main()
{
	len = 1;
	a[1] = '0';
	cin>>n;

	dfs(1);
	
	for(int i=1; i<=len; i++)
	{
		cout<<a[i];
	}
	cout<<endl;
	
	return 0;
}


发布了18 篇原创文章 · 获赞 0 · 访问量 128

猜你喜欢

转载自blog.csdn.net/weixin_44176696/article/details/103988190
今日推荐