NOJ1103-全排列
全排列
总提交 : 1148 测试通过 : 302
比赛描述
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
给定n个整数,现请编程求它们所有的全排列。
输入
输入包括两个行,第一行给出正整数n( 0 < n <=8), 第二个是 n个整数(大小范围:[-10^4, 10^4])。
输出
按字典序输出这n个整数的全排列,每一行给出一个全排列。
样例输入
3
1 23 88
样例输出
1 23 88
1 88 23
23 1 88
23 88 1
88 1 23
88 23 1
题目来源
NUPT
思路:非常简单的全排列问题。可以用DFS回溯求全排列,STL中有next_permutation( )这个函数更加方便。一定要先进行一个排序!不然Test1就卡住了。另外要注意输出格式,行末不能有多余空格,否则会出现output limite exceed这个结果。
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std; const int maxn = ;
int a[maxn]; int cmp( const void* a, const void* b ) {
return *( int* )a - *( int* )b;
} void printArray( int A[], int n ) {
int i;
//printf( "%d: ", ++count );
for( i = ; i < n - ; i++ ) {
printf( "%d ", A[i] );
}
printf( "%d", A[n - ] );
printf( "\n" );
} int main() {
int n;
scanf( "%d", &n );
for( int i = ; i < n; i++ ) {
scanf( "%d", &a[i] );
}
qsort( a, n, sizeof( a[]), cmp );
do {
printArray( a, n );
}while( next_permutation( a, a + n ) );
return ;
}
NOJ1103-全排列的更多相关文章
- 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)); 下 ...
随机推荐
- 【转】appium_python_API文档
1.contextscontexts(self): Returns the contexts within the current session. 返回当前会话中的上下文,使用后可以识别H5页面的控 ...
- 在Visual Studio 2013上安装SQLite designer components
最近搞一个WinCE项目,要用到SQLite.看人家都能直接在Visual Studio上连接SQLite.我也按照他们的方法安装了最新的Setups for 32-bit Windows (.NET ...
- canvas-7globleCompositeOperation2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android开发-API指南-<supports-gl-texture>
<supports-gl-texture> 英文原文:http://developer.android.com/guide/topics/manifest/supports-gl-text ...
- java.lang.NoSuchMethodException
这个异常遇到过若干次,提示信息也比较清楚的指示出它的特点,当无法找到某一特定方法时,就会抛出该异常! 我所遇到的抛出此异常的情景主要有以下两种: 1:对应的JAVA类中没有对应的属性,也就是说在页面的 ...
- 学习总结 JAVA环境配置 及其相应的步骤
相应的操作步骤 第一步: 右键我的电脑→属性→高级系统设置→环境变量→打开环境变量 第二步:在系统变量中添加JAVA_HOME ,并引用到相对应的地址. 第三步:在系统变量 path 中最前端(按ho ...
- ansible 访问内网服务器
ssh https://medium.com/@paulskarseth/ansible-bastion-host-proxycommand-e6946c945d30#.rauzlfv0z http: ...
- python线程使用场景 多线程下载
http://blog.xiayf.cn/2015/09/11/parallelism-in-one-line http://python.jobbole.com/84327/ http://www. ...
- jquery 设置元素内容html(),text(),val()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- zedboard如何从PL端控制DDR读写(二)——AXI总线
虽然Xilinx已经将和AXI时序有关的细节都封装起来,给出了官方IP和向导生成自定义IP,用户只需要关注自己的逻辑实现,但是还是有必要简单了解一下AXI的时序,毕竟咱是做硬件设计的. AXI(Ad ...