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)); 下 ...
随机推荐
- Spring Cloud 开门见山
Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性toke ...
- Beginning SDL 2.0(1) SDL功能简介
原文链接为 http://wiki.libsdl.org/Introduction. 由于近期整理音视频可视化的技术,发现好久不更新的SDL发布了2.0版本,以前也没有过于关注,这里准备尝试下.了解S ...
- Android7.0 拨号盘应用源码分析(一) 界面浅析
前言 android拨号盘的源码目录在package/app/Dialer 自7.0以后Incallui的源码直接放到了Dialer目录下,虽然在7.0以前incallui有自己独立的目录,但实际编译 ...
- express 4.x 模板引擎与express.static
前提:要在express中使用模块引擎需要将要使用的模板引擎安装在本项目,当然,express也是要安装的.在下面实例中,我使用的模板引擎是pug(一起叫做jade) 我的目录结构如下: 根目录为st ...
- Guid.NewGuid().ToString()的几种格式
1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid() ...
- MSP430F149学习之路——捕获/比较模式
1.捕获模式 #include <msp430x14x.h> unsigned ,last1=; unsigned ,j=; void mian(void) { WDTCTL = WDTP ...
- 网站导航不止有hao123!
网址导航对于我们上网而言非常的重要,在一定程度上决定了我们每天都在接触一些什么样的网络信息.我个人一直用的是hao123,总体感觉这个网址导航是非常的不错的,不过网址导航不只只有这一个好的更不只有这一 ...
- 使用UI Automation实现自动化测试 --工具使用
当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到 ...
- 详解android.mk-2016.01.18
1 Android.mk作用 当使用JNI开发时,我们需要创建一个native工程,Android.mk就是一个makefile的配置文件,帮助我们把编写的C/C++代码编译成动态或者静态的链接库. ...
- Completely change MACE timestamps?
Hi, One of my friends Sandy asked me about the possibility of completely change MACE timestamps. As ...