实现:

(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. AutoLayout具体解释+手把手实战

    首先说一下这篇博客尽管是标记为原创,可是事实并不是本人亲自写出来的.知识点和样例本人花了一天各处查找和整理终于决定写一个汇总的具体解释,解去各位朋友到处盲目查找的必要,由于不是转载某一个人的内容.故此 ...

  2. 解决 php7 cli 模式下中文乱码的两中方法

    解决 php7 cli 模式下中文乱码的两中方法1. 给PHP文件开头加上 exec('chcp 936'); 然后把该文件以 ANSI 格式编码2. 在 php.ini 中设置 default_ch ...

  3. TCP连接状态详解

    tcp状态: LISTEN:侦听来自远方的TCP端口的连接请求 SYN-SENT:再发送连接请求后等待匹配的连接请求 SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认 ES ...

  4. Http1.1和http2.0

    HTTP2.0 最近在读一本书叫<web性能权威指南>谷歌公司高性能团队核心成员的权威之作. 一直听说HTTP2.0,对此也仅仅是耳闻,没有具体研读过,这次正好有两个篇章,分别讲HTTP1 ...

  5. Android SecurityException

    public boolean checkNetwork() { boolean result = false; try { Context context = this.getApplicationC ...

  6. Exercise : Softmax Regression

    Step 0: Initialize constants and parameters Step 1: Load data Step 2: Implement softmaxCost Implemen ...

  7. C/C++(数据结构链表的实现)

    链表 List 链表实现了内存零碎片的有效组织. 静态链表 链表中有两个成员,数据域和指针域 数据域:我们存储的数据. 指针域:指针指向下一个具体的节点,代表了下一个节点的类型是链表类型. 所谓的指针 ...

  8. 【Pycharm】【HTML/jQuery】代码换行问题

    问题:从网上下载jQuery文件后发现代码太长,不利于阅读:Pycharm没有预先设置好js文件的自动换行设置 问题 解决办法 解决后

  9. c# winform 技术提升

    http://www.cnblogs.com/junjie94wan/category/303961.html http://www.cnblogs.com/springyangwc/archive/ ...

  10. HDU——T 4738 Caocao's Bridges

    http://acm.hdu.edu.cn/showproblem.php?pid=4738 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...