第一题s01字串
题意
我感觉题目的数据有一些错误
我自己写的代码(简单的实现题目):
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char s[100000];
int n;
void dfs(char s[],int len,int count){
if(count==n){
for(int i=0;i<len;i++){
printf("%c",s[i]);
}
//printf("\n%d",len);
return ;
}
for(int i=0;i<len;i++){
if(s[i]=='0'){
s[i]='1';
}
//if(s[i]=='1'){
else{
s[i]='0';
for(int j=len;j>i;j--){
s[j]=s[j-1];
}
s[i+1]='1';
len++;
i++;
}
}
dfs(s,len,count+1);
}
int main(int argc, char** argv) {
cin>>n;
s[0]='0';
dfs(s,1,0);
return 0;
}
参考了别人代码后,用insert函数更加简单:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main(int argc, char** argv) {
int n;
cin>>n;
string s="0";
while(n--)
{
for(int i=0;i<s.length();i++){
if(s[i]=='0'){
s[i]='1';
}
else {
s.insert(i,"0");
i++;
}
}
}
cout<<s;
return 0;
}
/*
还有用list集合(以及迭代器)的方法和上面差不多:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
list<int> l;
typedef list<int>::iterator iter;
int main(int argc, char** argv) {
int n;
cin>>n;
l.push_back(0);
while(n--)
{
for(iter i=l.begin();i!=l.end();i++){
if(*i==0){
*i=1;
}
else {
l.insert(i,0);
}
}
}
for(iter i=l.begin();i!=l.end();i++){
cout<<*i;
}
return 0;
}
/*