- #刷题
given a lowercase string 'ab', write a program to generate all possible lowe...

9151
#include <iostream>
#include <vector>
using namespace std;
void print(string& str, int n, int depth, vector<string>& re, string tmp)
{
if(depth == n)
{
re.push_back(tmp);
return;
}
tmp[depth] = str[depth];
print(str, n, depth+1, re, tmp);
tmp[depth] = str[depth] - 32;
print(str, n, depth+1, re, tmp);
}
int main()
{
string str("ab");
int n = str.size();
string tmp(n, '0');
vector<string> re;
print(str, n, 0, re, tmp);
for(auto ele:re)
cout<<ele<<endl;
return 0;
}
#include <vector>
using namespace std;
void print(string& str, int n, int depth, vector<string>& re, string tmp)
{
if(depth == n)
{
re.push_back(tmp);
return;
}
tmp[depth] = str[depth];
print(str, n, depth+1, re, tmp);
tmp[depth] = str[depth] - 32;
print(str, n, depth+1, re, tmp);
}
int main()
{
string str("ab");
int n = str.size();
string tmp(n, '0');
vector<string> re;
print(str, n, 0, re, tmp);
for(auto ele:re)
cout<<ele<<endl;
return 0;
}
1条回复
热度排序