实现:

(nm)
  • 既需要计算组合的总数 (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)的实现的更多相关文章

  1. [GCJ]Password Attacker

    https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...

  2. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  3. [Leetcode 39]组合数的和Combination Sum

    [题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...

  4. 377. Combination Sum IV

    问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...

  5. 计算一维组合数的java实现

    背景很简单,就是从给定的m个不同的元素中选出n个,输出所有的组合情况! 例如:从1到m的自然数中,选择n(n<=m)个数,有多少种选择的组合,将其输出! 本方案的代码实现逻辑是比较成熟的方案: ...

  6. C++单元测试 之 gtest -- 组合数计算.

    本文将介绍如何使用gtest进行单元测试. gtest是google单元测试框架.使用非常方便. 首先,下载gtest (有些google项目包含gtest,如 protobuf),复制目录即可使用. ...

  7. bzoj2982: combination(lucas定理板子)

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 664  Solved: 397[Submit][Status][Di ...

  8. [Leetcode 216]求给定和的数集合 Combination Sum III

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  9. 【LeetCode每天一题】Combination Sum II(组合和II)

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

随机推荐

  1. 紫书 例题 9-12 UVa 12186 (树形dp)

    这道题还是比较简单的,对于当前节点,算出每个儿子需要的人数 然后再算出当前节点需要多少个人数,然后排个序加上去就好了. #include<cstdio> #include<vecto ...

  2. 安装Apache PHP MySQL PHPMyAdmin

    视频教程:https://www.youtube.com/watch?v=FJC2iGt_2bc,Youtube看不了的FQ吧-3- 本人参考这篇文章:http://blog.csdn.net/kno ...

  3. CODEVS——T 4189 字典

    http://codevs.cn/problem/4189/  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Des ...

  4. VIjos——V 1782 借教室 | | 洛谷——P1083 借教室

    https://vijos.org/p/1782|| https://www.luogu.org/problem/show?pid=1083 描述 在大学期间,经常需要租借教室.大到院系举办活动,小到 ...

  5. &lt;LeetCode OJ&gt; 20. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. three.js 运行3D模型

    HTML  <!DOCTYPE html> <html style="height: 100%;"> <head> <title>m ...

  7. VMwarep挂载镜像及配置本地Yum源

    1.挂载镜像: *. 通过mount命令         linux mount挂载设备(u盘,光盘,iso等 )使用说明 *.  通过VMware的控制页面手工挂载 1.1    打开Vmware软 ...

  8. KnockOut下的离开检测

    <input type="text" class="form-control" data-bind="event:{ blur:$root.ch ...

  9. 使用PLupload在同一页面中进行多个不同类型上传解决方案和一次多文件上传的注意事项

    首先感谢,http://www.cnblogs.com/2050/p/3913184.html 这篇文章作者. 在使用PLUpload之前个人先封装了一些常用配置,并且将success与error做为 ...

  10. Android SDK使用国内镜像站,解决下载速度慢无法更新?

    1. 国内android开源镜像网站 下面是国内几个比較知名的开源网站.我用的是电子科技大学的镜像源,下载速度很快. mirrors.neusoft.edu.cn //东软信息学院 ubuntu.bu ...