全排列


Descriptions:

对于数组[1, 2, 3],他们按照从小到大的全排列是

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

现在给你一个正整数n,n小于8,输出数组[1, 2, …,n]的从小到大的全排列。

Input

输入有多行,每行一个整数。当输入0时结束输入。

Output

对于每组输入,输出该组的全排列。每一行是一种可能的排列,共n个整数,每个整数用一个空格隔开,每行末尾没有空格。

Sample Input

2

3

0

Sample Output

1 2

2 1

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

题目链接:

https://vjudge.net/problem/OpenJ_Bailian-4070

讲一个STL函数:

int 类型的next_permutation

int main()
{
int a[];
a[]=;
a[]=;
a[]=;
do
{
cout<<a[]<<""<<a[]<<""<<a[]<<endl;
}
while (next_permutation(a,a+)); //参数3指的是要进行排列的长度 //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继 }

输出:
 
 1 2 3
 1 3 2
 2 1 3
 2 3 1
 3 1 2
 3 2 1

如果改成 while(next_permutation(a,a+2));
则输出:
 1 2 3
 2 1 3
 
只对前两个元素进行字典排序
显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3

若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
 
 int list[3]={3,2,1};
next_permutation(list,list+3);
cout<<list[0]<<""<<list[1]<<""<<list[2]<<endl;
 
//输出: 1 2 3 

 
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <numeric>
using namespace std;
typedef long long ll;
int n;
int main()
{
while(cin >> n && n!=)
{
int a[];
for(int i=; i<n; i++)
a[i]=i+;
int j;
do
{
for(int i=; i<n; i++)
{
// 输出格式,特殊处理
if(i==n-)
cout<<a[i];
else
cout <<a[i]<< " ";
}
cout << endl;
}
while(next_permutation(a,a+n));
}
}

【OpenJ_Bailian - 4070 】全排列的更多相关文章

  1. PHP实现全排列(递归算法)

    算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为:    ① 如果n=1,则排列P只有一 ...

  2. hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

    xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串.                      (题于文末) 知识点: n个元素,其中a1,a2,··· ...

  3. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  5. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  7. 全排列算法的JS实现

    问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...

  8. java实现全排列

    前天上午的面试遇到了一个用java实现一串数字的全排列的题,想来想去用递归最方便,可是没有在规定的时间内完成555,今天上午有空便继续写,以下是完成后的代码: import java.util.Arr ...

  9. poj3187-Backward Digit Sums(枚举全排列)

    一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角)  3 1 2 4 //1~n 全排列中的一个排列  4 3 6  7 ...

随机推荐

  1. 【搜索引擎】Solr最新安装以及通过关系型数据库(MySQL,Oracle,PostgreSQL)导入数据

    版本号 最新的solr版本 : Solr 8.1.1下载地址:https://lucene.apache.org/solr/downloads.html solr-8.1.0.tgz for Linu ...

  2. POJ 2192 【DP】

    题意: 给三个字符串,判断前两个在相对顺序不变的情况下是否可以组成第三个字符串. 思路: 先说屌丝: dp[i][j]代表1串的前i个和2串的前j个字符在3串的前i+j个字符中最多能够组合出几个字符. ...

  3. 蓦然回首,Java 已经 24 岁了!

    01.蓦然 真没想到,Java 竟然 24 岁了(算是 90 后)! 提起 Java,印象最深刻的当然就是: class Cmower {  public static void main(Strin ...

  4. 【APUE】一个fork的面试题及字符设备、块设备的区别

    具体内容见:http://coolshell.cn/articles/7965.html 字符设备.块设备主要区别是:在对字符设备发出读/写请求时,实际的硬件I/O一般就紧接着发生了,而块设备则不然, ...

  5. Eureka 简介

    Eureka 简介

  6. curl 中文乱码

    curl 中文乱码 学习了:https://blog.csdn.net/thc1987/article/details/52583789 学习了: http://blog.itpub.net/2903 ...

  7. oracle 重要函数

    1)instr()函数的格式 (俗称:字符查找函数) 格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串) 格式二:instr( string1, st ...

  8. TF-IDF与余弦类似性的应用(一):自己主动提取关键词

    作者: 阮一峰 日期: 2013年3月15日 原文链接:http://www.ruanyifeng.com/blog/2013/03/tf-idf.html 这个标题看上去好像非常复杂,事实上我要谈的 ...

  9. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

  10. ios app抓包分析

    1 使用rvictl工具 这是mac下的一条命令.ios usb连mac,然后创建虚拟网络接口. 2 使用wireshark抓包 wireshark可以抓这个虚拟网络接口上的数据包.