zoukankan      html  css  js  c++  java
  • suseoj 1208: 排列问题 (STL, next_permutation(A.begin(), A.end()))

    1208: 排列问题

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 2  解决: 2
    [提交][状态][讨论版][命题人:liyuansong]

    题目描述

    全排列的生成就是对于给定的字符集或数集,用有效的方法将所有可能的全排列无重复无遗漏地枚举出来。对给定的字符集中的字符规定一个先后关系,在此基础上规定两个全排列的先后是从左到右逐个比较对应的字符的先后,或根据给定的数集中的大小关系,规定两个全排列的先后是从左到右逐个比较对应的数的大小,即依照字典序给出全排列。例如字符集{1,2,3},较小的数字较先,这样按字典序生成的全排列是:
    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

    思路:
      直接使用STL里面algorithm中的next_permutaion()方法

    核心代码:
      
    1 do{
    2     for(int i = 0; i < n; ++ i)
    3         printf("%d ", A[i]);
    4     printf("%d
    ", A[]n-1);
    5 }while(next_permutation(A, A+n));

    C/C++ 代码实现(AC):

      

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <cmath>
     6 #include <stack>
     7 #include <map>
     8 #include <queue>
     9 
    10 using namespace std;
    11 
    12 int main()
    13 {
    14     int n, A[10];
    15     scanf("%d", &n);
    16     for(int i = 0; i < n; ++ i)
    17         scanf("%d", &A[i]);
    18     sort(A, A+n, less<int>());
    19     do{
    20         for(int i = 0; i < n-1; ++ i)
    21             printf("%d ", A[i]);
    22         printf("%d
    ", A[n-1]);
    23     }while(next_permutation(A, A+n));
    24     return 0;
    25 }
     
  • 相关阅读:
    cf1100 F. Ivan and Burgers
    cf 1033 D. Divisors
    LeetCode 17. 电话号码的字母组合
    LeetCode 491. 递增的子序列
    LeetCode 459.重复的子字符串
    LeetCode 504. 七进制数
    LeetCode 3.无重复字符的最长子串
    LeetCode 16.06. 最小差
    LeetCode 77. 组合
    LeetCode 611. 有效三角形个数
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9054339.html
Copyright © 2011-2022 走看看