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)); 下 ...
随机推荐
- TextView里的文 html
一.[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android. ...
- "开发路上踩过的坑要一个个填起来————持续更新······(7月30日)"
欢迎转载,请注明出处! https://gii16.github.io/learnmore/2016/07/29/problem.html 踩过的坑及解决方案记录在此篇博文中! 个人理解,如有偏颇,欢 ...
- 【PL/SQL】异常处理:
如果在PLSQL块中没有做异常处理,在执行PLSQL块时,出现异常,会传递到调用环境,导致程序运行出错! SCOTT@ prod> declare v_ename emp.ename%type; ...
- SSH Secure Shell Client的傻瓜式使用方法
说明:本记录仅是使用此软件的一种简单的操作方式,如果想深入研究,请做如下三件事: 1)到其官网了解她的前世今生 2)下载她.安装她.操作她(这一步需要不断的尝试.不断的深入.不断的探索,当然最好理论结 ...
- 【HTML/XML 12】URI、URN、URL的联系和区别
导读:在学习XML的时候,书中有很多个地方都提到URL等几个概念,再之前做项目的时候,重定向或是转发时,也用到了这个URL,在学习Ajax时,ajax破坏了统一资源定位(URN)都或多或少的接触到了这 ...
- ionic cordova plugin simple demo
要用cordova plugin 的话还是需要设置一下的 1. 下载 ng-cordova.js download the zip file here 2. 在index.html 中引用 (cord ...
- navicat 连接oracle
Navicat远程连接oracle数据库数据库需要设置一下oci.dll. 下面介绍如何进行设置. 工具/原料 Navicat Pre instantclient-basiclite-nt-12. ...
- dell 网络产品线
https://en.wikipedia.org/wiki/Dell_Networking_Operating_System http://topics-cdn.dell.com/pdf/ DNOS ...
- gulp.spritesmith修改px为rem单位
移动端开发中,使用gulp.spritesmith进行小图sprite并生成样式,但由于spritesmith默认是以px为单位,所以就把插件的内容修改了下让生成rem单位并且能把background ...
- JS回车事件
<script type="text/javascript"> //当回车按下时,/=47,*=42,+=43 function keypress(form0){ if ...