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 ...
随机推荐
- 51nod 矩阵取数问题
一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值. f[i][j] = max(f[i-1][j], f[i][j-1]) + ...
- K-近邻算法学习
# -- coding: utf-8 -- from numpy import * import operator def createDataSet(): group = array([[1.0,1 ...
- C++的new_handler
这个new_handler其实对应于signal_handler 当operator new申请一个内存失败时,它会进行如下的处理步骤:1.如果存在客户指定的处理函数,则调用处理函数(new_hand ...
- 深度学习2015年文章整理(CVPR2015)
国内外从事计算机视觉和图像处理相关领域的著名学者都以在三大顶级会议(ICCV.CVPR和ECCV)上发表论文为荣,其影响力远胜于一般SCI期刊论文.这三大顶级学术会议论文也引领着未来的研究趋势.CVP ...
- SQL解析器的性能測试
对同一个sql语句,使用3种解析器解析出ast语法树(这是编译原理上的说法,在sql解析式可能就是解析器自己定义的statement类型).运行100万次的时间对照. package demo.tes ...
- C/C++获取本地时间常见方法
跨平台方法 方法一:手动暴力法 #include <iostream> using namespace std; #include <time.h> time_t t = ti ...
- Android 学习笔记之Bitmap位图虽触摸点移动
package xiaosi.bitmap; import android.app.Activity; import android.os.Bundle; public class mianActiv ...
- es6 -- Iterator 和 for...of 循环
1:Iterator(遍历器)的概念 JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Map和Set.这样就有了四种数据集合,用户还 ...
- PHP foreach遍历数组之如何判断当前值已经是数组的最后一个
先给出foreach的两种语法格式 1,foreach (array_expression as $value) statement 2,foreach (array_expression as $k ...
- 12、UVC&V4L2的关系
UVC是一种usb视频设备驱动.用来支持usb视频设备,凡是usb接口的摄像头都能够支持 V4L2是Linux下的视频采集框架.用来统一接口,向应用层提供API UVC: USB video clas ...