c++ combination by next_permutation
#include <iostream>
#include <algorithm>
#include <vector> int main() {
int n, r;
n = ;
r = ; std::vector<bool> v(n);
std::fill(v.end() - r, v.end(), true);
std::vector< std::vector<int> > sequence_vector; do {
std::vector<int> sequence;
for (int i = ; i < n; ++i) {
if (v[i]) {
std::cout << (i + ) << " ";
sequence.push_back(i+);
}
}
std::cout << "\n";
sequence_vector.push_back(sequence);
} while (std::next_permutation(v.begin(), v.end())); std::cout<<"from vector"<<std::endl; std::vector<int>::iterator iterator_sequence;
std::vector< std::vector<int> >::iterator iterator_sequence_vector;
for(iterator_sequence_vector = sequence_vector.begin();
iterator_sequence_vector != sequence_vector.end(); iterator_sequence_vector++){
for (iterator_sequence = (*iterator_sequence_vector).begin();
iterator_sequence != (*iterator_sequence_vector).end(); iterator_sequence++){
std::cout<<*iterator_sequence<<" ";
}
std::cout<<std::endl;
} return ;
}
c++ combination by next_permutation的更多相关文章
- [算法]——全排列(Permutation)以及next_permutation
排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...
- 全排列函数(next_permutation())
平常需要全排列的时候,一般都是dfs然后字符串匹配啥的……今天看题解的时候突然发现了这个神器. next_permutation()函数在c++的algorithm库里,作用是传入一个数组,输出这个数 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 55. 2种方法求字符串的组合[string combination]
[本文链接] http://www.cnblogs.com/hellogiser/p/string-combination.html [题目] 题目:输入一个字符串,输出该字符串中字符的所有组合.举个 ...
- 377. Combination Sum IV
问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
随机推荐
- CSU计算机研究生推免
考研复习 一开始,我是没有想到能够拿到研究生推免资格的,从今年3月份到整个暑假过完,一共6个月的时间,我一直在准备考研. 具体来说,我是在准备考研数学,整整6个月时间的数学复习,给我一种感觉,把大一大 ...
- fastJson简单实用
public class FastJsonText { @Test public void text(){ User user1 = new User(); user1.setName("健 ...
- Hive UDF 用户自定义函数 编程及使用
首先创建工程编写UDF 代码,示例如下: 1. 新建Maven项目 udf 本机Hadoop版本为2.7.7, Hive版本为1.2.2,所以选择对应版本的jar ,其它版本也不影响编译. 2. po ...
- NW.js开发环境的搭建
写在前面: 之前一直在找关于在mac怎么搭建nw.js的开发环境,苦于自己也没有很深入的理解,其实看看官方文档就差不多知道mac下要怎么整了. 官方文档的图: 正题开始: 先去下载一个nw.js的安装 ...
- jdbc连接各种数据库字符串
oracle driverClass:oracle.jdbc.driver.OracleDriver url:jdbc:oracle:thin:@127.0.0.1:1521:dbname mysql ...
- 【题解】洛谷P2426删数
链接 https://www.luogu.org/problemnew/show/P2426 念念碎 第一次接触到区间DP(瑟瑟发抖) 所以象征性地看了一下题解 这好像是一道比较基础的区间DP吧 但是 ...
- HDU1214 圆桌会议(找规律,数学)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1214 圆桌会议 Time Limit: 2000/1000 MS (Java/Others) M ...
- 我的wmware
1.vmware 网络连接方式 NAT 模式: 虚拟机的IP 是由NAT分配的,电脑环境无论如何变化,都不会影响虚拟机 好处:在家.学校.公司,连接虚拟机都可以使用相同的ip地址 桥接模式: 只要更换 ...
- Restframework中的Request
1.介绍 该篇博客主要介绍restframework内置的Request类,它扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等. 如: 在APIView中封装的r ...
- #leetcode刷题之路5-最长回文子串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1:输入: "babad"输出: "bab"注意: " ...