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)); 下 ...
随机推荐
- android 基础控件 EditText
EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...
- github添加ssh认证
总概:在使用git的时候,和目标仓库建立关系有两种方式https,ssh.一般用的是https认证(这样简单方便),但有个缺点,pull,push等操作需要频繁输入用户验证.虽然可以把用户验证账号密码 ...
- MATLAB 实用函数
MATLAB个人工具箱(MATLAB) mymail.m 可以利用MATLAB发送邮件(支持附件和群发),非常实用的函数,适用于:耗时很长的脚本完成后通知作者:外加定时器后用于信息推送:家庭监视器紧急 ...
- Selenium2.0介绍
selenium是一个web的自动化测试工具,和其它的自动化工具相比来说其最主要的特色是跨平台.跨浏览器. 支持windows.linux.MAC,支持ie.ff.safari.opera.chrom ...
- Nodejs文件服务器
最近一直在忙于一个比较大的项目,在项目中需要有个文件服务器来支持.老鸟们建议我去用NodeJs来实现,我在接手这个项目之前其实并不了解NodeJs,但是一直想去了解.借着这个机会好好去学习一下.下面是 ...
- 在git 服务器挂载新的项目
1.cmd 到新创建的文件夹位置,然后 执行该命令git init --bare 2.在客户端克隆项目到文件夹(空),将项目复制到该空文件夹下,然后打开git extent,提交并推送
- OSGI.NET mainfest.xml 配置
在使用 OSGI.NET进行插件式的开发时,需要对 Mainfest.xml 进行配置, Mainfest 文件是插件的重要配置文件,其中暴露了插件启动方式以及插件启动时所依赖的程序集或其它资源的信息 ...
- oracle的sqlldr并行导入表不要加索引
ORA-26002: Table string has index defined upon it. Cause: Parallel load was specified into a table w ...
- org.springframework.core.NestedIOException
今天遇到的一个小异常,报错信息如下: 2013-11-13 10:00:32 org.apache.catalina.core.StandardContext listenerStart严重: Exc ...
- Android基础总结(2)——活动Activity
1.什么是活动(Activity) 活动(Activity)是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用程序中可以包含零个或多个活动,但不包含任何活动的应用程序很少见. 2.怎么使用 ...