【C++】全排列
给定正整数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++】全排列的更多相关文章
- PHP实现全排列(递归算法)
算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为: ① 如果n=1,则排列P只有一 ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 全排列算法的JS实现
问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...
- java实现全排列
前天上午的面试遇到了一个用java实现一串数字的全排列的题,想来想去用递归最方便,可是没有在规定的时间内完成555,今天上午有空便继续写,以下是完成后的代码: import java.util.Arr ...
- poj3187-Backward Digit Sums(枚举全排列)
一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角) 3 1 2 4 //1~n 全排列中的一个排列 4 3 6 7 ...
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
随机推荐
- Redis-用思维导图二天搞定Redis用法。
Redis整体面貌 Redis基本数据结构 1.String 1.1 数据结构 long len byte数组长度 long free 可用数组长度 char buff[] 数据内容 1.2 命令 键 ...
- 软件包管理 rpm yum apt-get dpkg
http://blog.csdn.net/ljq1203/article/details/7401616
- mysql -h localhost和mysql -h 127.0.0.1的区别
今天早上同事说MySQL root账号登录不上了.我试了一下 #mysql -u root -p 提示”Access denied for user ‘root’@’localhost’ (using ...
- 測试oracle 11g cluster 中OLR的重要性
測试oracle 11g cluster 中OLR的重要性 called an Oracle Local Registry (OLR): each node in a cluster has a ...
- zookeeper集群,每个服务器上的数据是相同的,每一个服务器均可以对外提供读和写的服务,这点和redis是相同的,即对客户端来讲每个服务器都是平等的。
zookeeper集群,每个服务器上的数据是相同的,每一个服务器均可以对外提供读和写的服务,这点和redis是相同的,即对客户端来讲每个服务器都是平等的.
- 搞定android多点触摸模拟
原理在android 创建多点触摸虚拟设备,然后往设备写模拟数据可以
- WebKit 在 Windows 平台下编译小结
虽然WebKit 已经越来越多的被广大程序员接受,但其编译过程却非常之痛苦.下面将我编译WebKit 代码的经验与大家分享. 1) 获取WebKit 源代码 WebKit 源代码是使用Subversi ...
- 第十四章 openwrt 安装 python
需要安装libffi,python-mini,python.libffi以及python-mini需要安装在python之前 如果部分软件包不一样可以在下面的web后台搜索,搜索前先opkg ...
- Python调用windows下DLL详解
Python调用windows下DLL详解 - ctypes库的使用 2014年09月05日 16:05:44 阅读数:6942 在python中某些时候需要C做效率上的补充,在实际应用中,需要做部分 ...
- BP反向传播算法的工作原理How the backpropagation algorithm works
In the last chapter we saw how neural networks can learn their weights and biases using the gradient ...