给定正整数n,求1,2,3,...,n的全排列

解法一:递归,结果并不为字母序排列。

void Helper(vector<int> v, int low, int high)
{
if(low == high)
{
for(int i = ; i < v.size(); i ++)
cout << v[i];
cout << endl;
}
else
{
for(int i = low; i <= high; i ++)
{
swap(v[low], v[i]);
Helper(v, low+, high);
swap(v[low], v[i]);
}
}
}
void permutation(int n)
{
vector<int> v(n,);
for(int i = ; i < n; i ++)
v[i] = i+;
Helper(v, , n-);
}

解法二:algorithm库中的next_permutation函数,结果按字母序排列

void permutation(int n)
{
vector<int> v(n,);
for(int i = ; i < n; i ++)
v[i] = i+;
do
{
for(int i = ; i < v.size(); i ++)
cout << v[i];
cout << endl;
}while(next_permutation(v.begin(),v.end()));
}

【C++】全排列的更多相关文章

  1. PHP实现全排列(递归算法)

    算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为:    ① 如果n=1,则排列P只有一 ...

  2. hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

    xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串.                      (题于文末) 知识点: n个元素,其中a1,a2,··· ...

  3. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  5. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  7. 全排列算法的JS实现

    问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...

  8. java实现全排列

    前天上午的面试遇到了一个用java实现一串数字的全排列的题,想来想去用递归最方便,可是没有在规定的时间内完成555,今天上午有空便继续写,以下是完成后的代码: import java.util.Arr ...

  9. poj3187-Backward Digit Sums(枚举全排列)

    一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角)  3 1 2 4 //1~n 全排列中的一个排列  4 3 6  7 ...

  10. 关于全排列 next_permutation() 函数的用法

    这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...

随机推荐

  1. 使用Mysql慢查询日志对有效率问题的SQL进行监控

    输入命令:show variables like 'slow%' 可以发现 slow_query_log 为 OFF(默认),表示未开启慢查询日志 slow_query_log_file 为慢查询日志 ...

  2. Colorful Lecture Note

    Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorithm lectu ...

  3. UML类图符号 各种关系说明以及举例(转载)

    文章出处:http://www.cnblogs.com/duanxz/archive/2012/06/13/2547801.html UML中描述对象和类之间相互关系的方式包括:依赖(Dependen ...

  4. JAVA多线程读写文件范例

    在写之前先声明,本文是基于之前在博客园网站上检索到的一份JAVA多线程读写文件的示例,我在写自己的程序时是在那位作者写的基础上做了改良,但已不记得原文的地址.如果有知情者,烦请帖出地址,我在此文上加入 ...

  5. Windows Server 2012 R2 两个奇葩问题的解决

    1. 服务管理器一打开就崩溃此问题由Intel HD Graphics显卡驱动不兼容引起,卸载掉驱动,仅使用微软基本显示驱动.等待Intel更新符合WDDM 1.3规范的驱动 2. 不能设置静态IP. ...

  6. HelloWorld 和相关设置

    写这篇文章的初衷很简单,就是想再一次证明 IntelliJ IDEA 对于 Java 开发人员来说,确实比 eclipse 要好用得多,鉴于目前市面上关于 IntelliJ IDEA 的教程比较少,叙 ...

  7. Install Linux Kernel 4.10 In CentOS and Ubuntu

    https://www.ostechnix.com/install-linux-kernel-4-10-centos-ubuntu/

  8. hdu4035之经典慨率DP

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submi ...

  9. 【spring cloud】spring cloud 使用feign调用,1.fallback熔断器不起作用,2.启动报错Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanica.aop.aspectj.Hystri解决

    示例GitHub源码地址:https://github.com/AngelSXD/springcloud 1.首先使用feign调用,需要配置熔断器 2.配置熔断器需要将熔断器注入Bean,熔断器类上 ...

  10. OpenSSL再曝CCS注入漏洞-心伤未愈又成筛子

    太戏剧了,昨晚看了佳片有约,还不错,2012版的<完美回顾>,像我这样的人依旧选择用电视或者去影院看电影,在没有中间插播广告的时候,体验憋尿得过程中,总是能突然有非常多的想法,这是用电脑或 ...