c++组合数学
c++ combinatorics
我需要c++代码,它将生成所有可能的组合(n,k)和repositions其中n-输入数组中整数的数量。k-的位置数量
例如输入:
n = [1 2 3];
k = 2;
输出:
A3 =
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
谢谢。
使用标准库:
do {
for(int i = 0; i < k; i++){
std::cout << n[i];
}
std::cout << 'n';
} while (std::next_permutation(n, n + k));
请在此处查看我的答案:
PHP接受所有组合
它是PHP;但这个概念(递归等)应该很容易"翻译"。。。
这基本上是以n-1为基数计数(其中每个数字都偏移1),请尝试以下操作:
编辑:使用vector
而不是new[]
、delete[]
#include <vector>
void generatePerms(int n, int k)
{
vector<int> perms(k, 1);
//iterate through all permutations
bool done;
do {
//Do something with the current permutation, for example print it:
for (int i = 0; i < k-1; i++)
cout << perms[i] << ", ";
cout << perms[k-1] << endl;
/*
* Increment last digit first - if it's to big, reset to 1 and
* carry one (increment next digit), which may also carry one etc.
*
* If all digits caused a carry, then the permutation was n, n, ..., n,
* which means, that we can stop.
*/
done = true;
for (int i = k-1; i >= 0; i--) {
if (++perms[i] > n) {
perms[i] = 1;
continue;
} else {
done = false; //not all digits caused carry
break;
}
}
} while (!done);
}
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 通过组合不同的类型来创建唯一的id
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 模板元编程:如何将参数包组合成新的参数包
- 检查向量是否具有所有可能的字符组合
- 如何在加密++中将两个源组合成新的源
- 根中的组合
- 更改 C++ 中的组合分类变量
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 组合字符串不适用于 libCurl,C++
- 测试两个类型列表中的所有组合
- 将多个 for 循环组合成单个迭代器
- 如何从组合指数中找到仓位
- 防止组合框被关闭
- 如何解决可能的唯一组合问题