C++ 写一个简短的C++;输出所有可能的字符串的程序,这些字符串使用每个字符’;a’’;b&x2019’;c’’;d&x2019’;e’;,和’;f’;正好一次
我遇到了这个问题,但我无法解决它。我能编写的所有代码都是像ab、ac、ad、ae、af之类的小字符串。但不适用于较长的字符串,如abc、abcd等。如果有人能指导我找到某种解决办法,那就太好了。我希望不使用递归,但如果没有,则递归也可以 这是我的密码:C++ 写一个简短的C++;输出所有可能的字符串的程序,这些字符串使用每个字符’;a’’;b&x2019’;c’’;d&x2019’;e’;,和’;f’;正好一次,c++,C++,我遇到了这个问题,但我无法解决它。我能编写的所有代码都是像ab、ac、ad、ae、af之类的小字符串。但不适用于较长的字符串,如abc、abcd等。如果有人能指导我找到某种解决办法,那就太好了。我希望不使用递归,但如果没有,则递归也可以 这是我的密码: #include<iostream> #include<vector> #include<algorithm> using namespace std; vector<string> make_
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> make_string(vector<char>vec, char ch)
{
int i=0;
vec.erase(remove(vec.begin(), vec.end(), ch), vec.end());
int size = vec.size();
vector<string>all_strings;
string answer="";
for(i=0;i<size;i++) //here is the "meat". I could add a few more for loops for longer strings
// But I think that would just get messy.
{
answer= answer+ch+vec[i];
all_strings.push_back(answer);
answer="";
}
return all_strings;
}
void print_vector(vector<string>vec)
{
int i=0;
int size = vec.size();
for(i=0;i<size;i++)
{
cout<<vec[i]<<endl;
}
cout<<"--------------------------";
cout<<endl;
}
int main()
{
vector<char>vec;
vec.push_back('a');
vec.push_back('b');
vec.push_back('c');
vec.push_back('d');
vec.push_back('e');
vec.push_back('f');
int i=0;
vector<string>my_strings;
int size=vec.size();
for(i=0;i<size;i++)
{
my_strings=make_string(vec,vec[i]);
print_vector(my_strings);
my_strings.clear();
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
向量生成_字符串(向量向量、字符)
{
int i=0;
删除(删除(vec.begin(),vec.end(),ch),vec.end());
int size=vec.size();
矢量所有_字符串;
字符串答案=”;
对于(i=0;i您正在寻找一种置换算法。请查看wordaligned.org上的这篇文章,它描述了该问题的迭代解决方案:
作者的代码非常简单,并使用了标准库:
#include <algorithm>
#include <cstdio>
int main()
{
char xs[] = "abcdef"; // <-- modified to fit the question.
do
{
std::puts(xs);
}
while (std::next_permutation(xs, xs + sizeof(xs) - 1));
return 0;
}
#包括
#包括
int main()
{
char xs[]=“abcdef”;//对于“每个字符仅一次”,您可以在算法头中使用std::next_排列。对于“atmost once”,您必须尝试其他方法。