实现:

(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. 搭建专属于自己的Leanote云笔记本

    搭建专属于自己的Leanote云笔记本 Leanote 依赖 MongoDB 作为数据存储,下面开始安装 MongoDB: 下载 MongoDB 进入 /home 目录,并下载 MongoDB: cd ...

  2. 二进制部署mysql5.6

    二进制部署不用编译直接配置环境,初始化就可以使用了下面是官网给的方法: MySQL 二进制安装解决依赖yum install libaio shell> yum search libaio # ...

  3. 【Henu ACM Round#15 C】 A and B and Team Training

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举第一种方法. 剩下的全都个第二种方法. 看看能组成多少个队伍就可以了. [代码] #include <bits/stdc+ ...

  4. CCF模拟题 相反数

    相反数 时间限制: 1.0s 内存限制: 256.0MB 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数).   输入格式 第一行包含一个 ...

  5. Python Web框架Tornado的异步处理代码演示样例

    1. What is Tornado Tornado是一个轻量级但高性能的Python web框架,与还有一个流行的Python web框架Django相比.tornado不提供操作数据库的ORM接口 ...

  6. vim-缓存区中打开另外一个文件的方法

    现在有这么一种情况:    我现在在ubuntu用户根目录下--    我根目录下有一个文件夹blogs,这个文件夹下面有两个文件:text1,text2.    我现在从-目录下进行如下操作    ...

  7. es6 -- Iterator 和 for...of 循环

    1:Iterator(遍历器)的概念 JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Map和Set.这样就有了四种数据集合,用户还 ...

  8. javaweb三、JDBC访问数据库

    JDBC是J2SE的内容,是由java提供的访问数据库的接口,但没有提供具体的实现方法,需要数据库厂商提供,就是对应的数据库驱动. 这样的好处是可以方便的更换数据库,提高了扩展性.这也是面向接口编程的 ...

  9. mount 命令

    命令格式:mount [-t vfstype] [-o options] device dir 嵌入式设备挂载命令mount -o nolock -t nfs 192.168.1.24:/home/t ...

  10. js数组详解:

    一. 数组的浅复制与深复制: 数组之间的复制,由于数组是引用类型,如果是字面量式复制,导致只要是改变其中一个数组的值两者都会发生变化,这种复制叫做浅复制.如果要想复制后不收影响,则需要深复制.深复制就 ...