C++组合数(combination)的实现
实现:
- 既需要计算组合的总数 (32)=3;
- 也需要分别获得每一种组合的情形,用于穷举搜索;
- 1, 2; 1, 3; 2, 3
1. 递归实现
// picked + toPick == m
void comb(int n, vector<int>& picked, int toPick){
if (toPick == 0) { printPicked(picked); return; }
int smallest = picked.empty() ? 0 : picked.back() + 1;
for (int next = smallest; next < n; ++next){
picked.push_back(next);
comb(n, picked, toPick-1);
picked.pop_back();
// 关键!!!
}
}
对于 (42) 而言,四个之中选 2 个,调用端代码如下,
vector<int> picked; // 开始为空;
comb(4, picked, 2);
// 第一个参数表示 n = 4,第三个参数表示 m=2
最终的输出结果为:
0 1
0 2
0 3
1 2
1 3
2 3
也即,
- 0 先进数组,1 再进数组 ⇒ toPick == 0, 输出 (0, 1)
- 1 出数组,2 进数组 ⇒ toPick == 0, 输出 (0, 2)
- 2 出数组,3 进数组 ⇒ toPick == 0, 输出 (0, 3)
- 3 出数组,0 出数组 ⇒ next ⇒ 1
- …
C++组合数(combination)的实现的更多相关文章
- [GCJ]Password Attacker
https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- 377. Combination Sum IV
问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...
- 计算一维组合数的java实现
背景很简单,就是从给定的m个不同的元素中选出n个,输出所有的组合情况! 例如:从1到m的自然数中,选择n(n<=m)个数,有多少种选择的组合,将其输出! 本方案的代码实现逻辑是比较成熟的方案: ...
- C++单元测试 之 gtest -- 组合数计算.
本文将介绍如何使用gtest进行单元测试. gtest是google单元测试框架.使用非常方便. 首先,下载gtest (有些google项目包含gtest,如 protobuf),复制目录即可使用. ...
- bzoj2982: combination(lucas定理板子)
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 664 Solved: 397[Submit][Status][Di ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- 【LeetCode每天一题】Combination Sum II(组合和II)
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- C++的new_handler
这个new_handler其实对应于signal_handler 当operator new申请一个内存失败时,它会进行如下的处理步骤:1.如果存在客户指定的处理函数,则调用处理函数(new_hand ...
- openssl之EVP系列之5---EVP_Encrypt系列函数具体解释(二)
openssl之EVP系列之5---EVP_Encrypt系列函数详细解释(二) ---依据openssl doc/crypto/EVP_EncryptInit.pod和doc/ssleay.t ...
- [React] Stop Memory Leaks with componentWillUnmount Lifecycle Method in React
In this lesson we'll take a stopwatch component we built in another lesson and identify and fix a me ...
- [Python] Finding the most common elements in an iterable
>>> import collections >>> # Tally occurrences of words in a list >>> cnt ...
- Handle-postDelayed 延迟操作
今天在工作的时候,遇到了一个方法,是关于Handle来实现延时操作的,自己写了一个小demo,学习总结如下 xml <?xml version="1.0" encoding= ...
- Excel 2013数据挖掘工具栏的介绍(二)
这里不多说,直接上干货! 上一篇博客是 下载安装与配置Excel 2013数据挖掘加载项(SQL Server 2012 SP1 + SQLServer2012_DMAddin.msi) Excel ...
- notepad++go语法高亮文件
notepad++go语法高亮文件 下载 右键另存为下载后在语言栏中的自定义面板中直接导入,重启即可
- Kinect 开发 —— 保持视频影像
相比直接将影像显示出来,如果能将录制到的影像保存到硬盘上就好了.但是,影像录制,是需要一定的技巧,在网上可以看到很多例子演示如何将Kinect获取到的影像以图片的形式保存到本地,前面的博文也介绍了这一 ...
- 杭电(hdu)2053 Switch Game 水题
Switch Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 点击事件-click,longclick
今天在修改一个问题的时候,遇到了click,longclick事件触发情况.记录下来. 代码 tView.setOnLongClickListener(new OnLongClickListener( ...