【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)); 下 ...
随机推荐
- mybatis源码分析(2)-----SqlSession创建
1. 在创建好sqlSessionFactory之后,接着就要配置sqlSession的创建. <bean id="simpleTempalte" class="o ...
- PSCollectionView瀑布流实现
[-] 一基本原理 二具体实现 相关数据结构 视图更新方式 relayoutViews方法 removeAndAddCellsIfNecessary方法 select方法 重用数据块视图机制 三使用方 ...
- Programmable current source requires no power supply
Engineering labs are usually equipped with various power supplies, voltmeters, function generators, ...
- springmvc 配置和spring配置?
最近在接触mybatis,之间使用springmvc时,配置文件一直是,web.xml+XX-servlet.xml 的配置(xx为web.xml中servlet name名称).为了整合mybati ...
- Socket INADDR_ANY详解
转载:http://hi.baidu.com/zorro_knight/item/37af9e8c9dc71253e73d1924 linux下的socket INADDR_ANY表示的是一个服务器上 ...
- 【IntellJ IDEA】idea上所有代码都报错了
可能会碰到蓝屏,内存溢出重启idea等特殊情况. 重新打开idea后发现原本的代码全都报错了 正确的解决方法: 方法很简单 执行idea工具栏上下面的菜单: File -> Invalidate ...
- Table里td中的文本过长,设置不换行,随内容同行显示(转载)
当td中内容过长时,内容会溢出,换行显示,美观超级差,在td里设置这个属性 "white-space:nowrap 就可以解决排版问题啦 <td style="white ...
- sql数据库出现可疑
USE master GO SP_CONFIGURE 'allow updates',1 RECONFIGURE WITH OVERRIDE GO UPDATE SYSDATABASES SET ST ...
- JQuery EasyUI 动态改变表单项的验证守则
//JQuery EasyUI 动态改变表单项的验证规则 $(document).ready(function(){ $('#FILE_QUALITY').combobox({ onChange:fu ...
- 扩展JQUERY 表单加载JSON数据
$.fn.extend({ //表单加载json对象数据 setForm : function (jsonValue) { var obj = this; $.each(jsonValue, func ...