21. 1~n任意取数组成m
来源:互联网 发布:淘宝孕妇装 编辑:程序博客网 时间:2024/04/29 08:55
输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数,
This is a combination generation problem.
使其和等于m ,要求将其中所有的可能组合列出来.
HANDWRITING:
void search(int n, int m, int *com, int pos) {if (m == 0) {for (int i = pos - 1; i >= 0; --i) cout<<com[i]<<" ";cout<<endl;}int s = m < n ? m : n;while (s > 0) {com[pos] = s;search(s - 1, m - s, com, pos + 1);--s;}}void combination (int n, int m) {int *com = new int[n];search(n, m, com, 0);delete [] com;}
This is a combination generation problem.
void findCombination(int n, int m) { if (n>m) findCombination(m, m); int aux[n]; memset(aux, 0, n*sizeof(int)); helper(m, 0, aux);}void helper(int dest, int idx, int aux[], int n) { if (dest == 0) dump(aux, n); if (dest <= 0 || idx==n) return; helper(dest, idx+1, aux, n); aux[idx] = 1; helper(dest-idx-1, idx+1, aux, n); aux[idx] = 0;}void dump(int aux[], int n) { for (int i=0; i<n; i++) if (aux[i]) printf(“%3d”, i+1); printf(“\n”);}PS: this is not an elegant implementation, however, it is not necessary to use gray code or other techniques for such a problem, right?
- 21. 1~n任意取数组成m
- 求从1,2...n中取任意个不重复的数和为m的所有组合
- 【二进制枚举】求在n个数中取任意个数加起来组成x的方案数
- 21.输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数
- 给定数m,求由n位数的组成m的数列(m>=n>0)
- 任意给定一个自然数N,要求M是N的倍数,且他的所有各位数字都是由0或1组成,并要求M尽可能小
- 从数列1,2,3.......n 中 随意取几个数,使其和等于 m
- 在[1,n]中随机取m个无重复数的Bob Floyd算法
- 从数列1,2,3.......n 中 随意取几个数,使其和等于 m
- 从数列1,2,3.......n 中 随意取几个数,使其和等于 m
- 从数列1,2,3.......n 中 随意取几个数,使其和等于 m
- 从数列1,2...n中随意取几个数,使其和等于m
- 求1到n中取几个数使和为m
- m个数取n个的排列数 Cmn
- 输入两个整数n和m, 从数列1,2,...,n中任意选择几个数,使其和等于m, 要求编写程序输出所有的组合
- 输入两个整数n和m, 从数列1,2,...,n中任意选择几个数,使其和等于m, 要求编写程序输出所有的组合
- 输入两个整数n和m, 从数列1,2,...,n中任意选择几个数,使其和等于m, 要求编写程序输出所有的组合
- 已知m,n是正整数,编写程序求m中取n的组合数
- 字符串匹配算法
- Cocos2d-x 一边执行动作序列一边播放动画
- 给定一个数组,数组中有正负数,求出所有字数组中和值最大的值。
- spring中如何处理oracle大字段
- [各种面试题] 复制带随机节点的链表
- 21. 1~n任意取数组成m
- 《计算机网络》 读书笔记(六) 协议比较
- 寄存器UART
- 教你如何成为数据科学家(九)
- rsh 配置
- 程序员面试宝典(第5章)部分考题
- config 与 makefile
- 子数组的最大乘积
- C++中的头文件