如何编写'n'字符串的所有组合?重复:C=n!/(n-k)!
How can I write all the combinations of 'n' strings? With Repetition: C=n!/(n-k)!
对不起,如果问题不清楚,我想知道如何在c++中编写一个程序,可以输出一些句子的所有组合,使用公式C=n!/(n-k)!
。例如,这就是我想要打印的东西:
combination no 1: sentence1 sentence2 sentence3 sentence4
combination no 2: sentence1 sentence2 sentence4 sentence3
combination no 3: sentence1 sentence3 sentence2 sentence4
combination no 4: sentence1 sentence3 sentence4 sentence2
combination no 5: sentence1 sentence4 sentence3 sentence2
combination no 6: sentence1 sentence4 sentence2 sentence3
And so on...
还有,是否可能有多达10亿个组合,或者有一些限制?
编辑。
我试了下面的程序,但是我找不到一个方法来改变上面公式中的"k"变量。
// next_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
#include <string> // std::string
#include <vector> // std::vector
int main () {
std::string sentence1 = " A Sentence number one ";
std::string sentence2 = " B Sentence number two ";
std::string sentence3 = " C Sentence number three ";
std::string sentence4 = " D Sentence number four ";
// Store all the elements in a container ( here a std::vector)
std::vector<std::string> myVectorOfStrings;
// In the vector we add all the sentences.
// Note : It is possible to do myVectorOfStrings.push_back("Some sentence");
myVectorOfStrings.push_back(sentence1);
myVectorOfStrings.push_back(sentence2);
myVectorOfStrings.push_back(sentence3);
myVectorOfStrings.push_back(sentence4);
// The elements must be sorted to output all the combinations
std::sort (myVectorOfStrings.begin(),myVectorOfStrings.end());
std::cout << "The 4! possible permutations with 4 elements:n";
do {
//This printing can be improved to handle any number of sentences, not only four.
std::cout << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << 'n';
} while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );
std::cout << "After loop: " << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << 'n';
return 0;
}
您的意思可能是需要'n'字符串的所有可能组合。有n个!可能的情况下。您可以使用std::next_permutation方法如下:
我想你所有的句子都是这样的std::string:
// next_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
#include <string> // std::string
#include <vector> // std::vector
int main () {
std::string sentence1 = " A Sentence number one ";
std::string sentence2 = " B Sentence number two ";
std::string sentence3 = " C Sentence number three ";
std::string sentence4 = " D Sentence number four ";
// Store all the elements in a container ( here a std::vector)
std::vector<std::string> myVectorOfStrings;
// In the vector we add all the sentences.
// Note : It is possible to do myVectorOfStrings.push_back("Some sentence");
myVectorOfStrings.push_back(sentence1);
myVectorOfStrings.push_back(sentence2);
myVectorOfStrings.push_back(sentence3);
myVectorOfStrings.push_back(sentence4);
// The elements must be sorted to output all the combinations
std::sort (myVectorOfStrings.begin(),myVectorOfStrings.end());
std::cout << "The 4! possible permutations with 4 elements:n";
do {
//This printing can be improved to handle any number of sentences, not only four.
std::cout << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << 'n';
} while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );
std::cout << "After loop: " << myVectorOfStrings[0] << ' ' << myVectorOfStrings[1] << ' ' << myVectorOfStrings[2] << ' ' << myVectorOfStrings[3] << 'n';
return 0;
}
这是一个简单的打印示例。如果你有超过4个字符串,在do-while循环中你会使用这样的语句
do-while循环应该是:
do {
//Print all the sentences in my vector :
for( auto i = myVectorOfStrings.begin(); i != myVectorOfStrings.end(); ++i)
std::cout << *i << ' ';
// Go to the next line
std::cout << std::endl;
} while ( std::next_permutation(myVectorOfStrings.begin(),myVectorOfStrings.end()) );
还有,是否可能有多达10亿个组合呢有限制吗?
唯一的限制是内存。在这个例子中,你只有一个存储所有字符串的向量。所以如果你有10根弦,你就有10根!= 3,628,800种不同的组合,但内存本身只是你的向量使用的内存,有10个字符串
您可以使用next_permutation
来完成此操作
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 通过组合不同的类型来创建唯一的id
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 模板元编程:如何将参数包组合成新的参数包
- 检查向量是否具有所有可能的字符组合
- 如何在加密++中将两个源组合成新的源
- 根中的组合
- 更改 C++ 中的组合分类变量
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 组合字符串不适用于 libCurl,C++
- 测试两个类型列表中的所有组合
- 将多个 for 循环组合成单个迭代器
- 如何从组合指数中找到仓位
- 防止组合框被关闭
- 如何解决可能的唯一组合问题