在 c 中给定一个固定数的情况下,找到所有可能的加法和组合(给定一个总和,找到它的可能的加法和排列
Finding all possible addition and combination given a fixed number in c ( given a sum, find the possible addition and permutation of it.)
我正在寻找所有可能的数字组合以及它们在固定大小的数组中的排列。例如,我有总共 5 个,所需的输出将是
5 0 0 0
05 0 0
0 05 0
0 00 5
4 1 0 0
1 4 0 0
1 0 4 0
1 0 0 4
01 4 0
等等...这可能吗?
使用递归:
void find(int remain, int depth, vector<int> v) {
if (depth == 4) {
if (remain == 0){
for (int i = 0 ;i < 4; i++) {
cout << v[i];
}
cout<<endl;
}
return;
}
for (int i = 0; i <= remain; i++) {
v.push_back(i);
find(remain-i, depth+1, v);
v.pop_back();
}
}
相关文章:
- 在 c 中给定一个固定数的情况下,找到所有可能的加法和组合(给定一个总和,找到它的可能的加法和排列
- 有没有一种 STL 方法可以找到字符串的所有排列,给出一个以 C++ 为单位的大小?
- 下一个排列定义
- 最大数量.给定一个非负整数列表,排列它们以使它们形成最大的数字
- 给定一个包含 n 个整数和一个整数 x 的未排序数组 A,重新排列 A 中的元素
- Rcpp:按另一个向量的顺序重新排列一个向量
- C 按顺序排列一个指针阵列
- 检查一个字符串是否是另一个字符串的排列C++
- 如何通过C 中的另一个索引来重新排列数组
- C++ STL 下一个组合排列
- 在linux中动态地将应用程序窗口排列在一个又一个之上
- 我编写了一个C++程序来打印单词字母的排列.我想知道是否有其他更简单的方法可以做到这一点
- C++-是否可以使用random_shuffle生成数组的每一个排列,一次且仅一次
- 有没有一个函数像next_placement,但对于具有重复的排列
- c++重新排列一个整数
- 制作一个排列函数,输出数组中所有可能的排列
- 如何使用next_permutation只有一个排列
- 给定一个字符串,在字典中找出它的所有组成单词的排列
- 排序三个向量,基于一个的排列,有重复项
- 我正在创建一个双链接列表,它将按字母顺序排列姓名列表,但我不确定在int main()函数中放入什么