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)); 下 ...
随机推荐
- selenium借助AutoIt识别上传文件Java篇
官方网站:https://www.autoitscript.com/site/ 从网站上下载AutoIt并安装,安装完成在菜单中会看到图4.13的目录: 图4.13 AutoIt菜单 1.首先打开A ...
- mysql创建远程用户
grant all privileges on *.* to myuser@"%" identified by 'password'; 用root用户登陆,然后: grant al ...
- autoit UIA获取Listview的信息
#include "CUIAutomation2.au3" Opt( ) Global $oUIAutomation MainFunc() Func MainFunc() ; Be ...
- 【练习】ORACLE统计信息--直方图
①创建表tSQL> create table t as select * from dba_objects; Table created. --收集直方图 SQL> exec dbms_s ...
- 4种kill某个用户所有进程的方法
在linux系统管理中,我们有时候需要kill掉某个用户的所有进程,初学者一般先查询出用户的所有pid,然后一条条kill掉,或者写好一个脚本,实际上方法都有现成的,这边有4种方法,我们以kill用户 ...
- 重温CSS:Border属性
边界是众所周知的,有什么新的东西吗?好吧,我敢打赌,在这篇文章中,有很多你不看永远不知道的东西! 不仅可以用CSS3来创建圆角,使用原有CSS一样可以显示自定义图形.这是正确的(有待考究):在过去,没 ...
- ububtu 14.04 问题集合
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4168168.html 1.Chromium 中的flash插件问题: sudo apt-ge ...
- MyEclipse中使用debug调试程序
最基本的操作是: 1.首先在一个java文件中设断点,然后debug as-->open debug Dialog,然后在对话框中选类后--> Run 当程序走到断 ...
- cpack
一. 简介 CPack是CMake 2.4.2之后的一个内置工具,主要作用就是生成制定类型的安装包.它可以脱离cmake单独运行. 二. 基本设置 (mandatory) 设置包类型set(CPACK ...
- JqueryMobile动态生成listView并实现刷新的两种方法
本篇文章主要是对JqueryMobile动态生成listView并实现刷新的两种方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 JqueryMobile动态生成listView并实现刷新 ...