zoukankan      html  css  js  c++  java
  • [OI

    next_permutation( ) 和 prev_permutation( ) 函数基本类似,都需要用到头文件名<algorithm>

    next_permutation()函数

    用法:next_permutation(first,last)

    作用:next_permutation()函数将 [ first , last ] 区间中的序列转换为字典序的下一个排列。如果下一个排列存在返回true,如果下一个排列不存在(即区间中包含的是字典序的最后一个排列),则该函数返回false,并将区间转换为字典序的第一个排列。

    代码实现

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 //#define DEBUG(x) cerr << #x << "=" << x << endl
     8 const int maxn = 1e5 + 10;
     9 
    10 int n, f[maxn];
    11 int main()
    12 {
    13     //ios::sync_with_stdio(false);
    14     cin.tie(0);
    15     cin >> n;
    16     for (int i = 0; i < n; i++) cin >> f[i];
    17     sort(f, f + n);
    18     do
    19     {
    20         for (int i = 0; i < n; i++) cout << f[i] << " ";
    21         //cout << endl;
    22         puts("");
    23     }while (next_permutation(f, f + n)); 
    24     return 0;
    25 } 

     prev_permutation()函数

    用法:prev_permutation(first,last)

    作用:prev_permutation()函数将 [ first , last ] 区间中的序列转换为字典序的上一个排列。如果上一个排列存在返回true,如果上一个排列不存在(即区间中包含的是字典序的第一个排列),则该函数返回false,并将区间转换为字典序的最后一个排列。

    代码实现

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 //#define DEBUG(x) cerr << #x << "=" << x << endl
     8 const int maxn = 1e5 + 10;
     9 
    10 int n, f[maxn];
    11 
    12 int cmp(int a, int b)
    13 {
    14     return a > b;
    15 }
    16 
    17 int main()
    18 {
    19     //ios::sync_with_stdio(false);
    20     cin.tie(0);
    21     cin >> n;
    22     for (int i = 0; i < n; i++) cin >> f[i];
    23     sort(f, f + n, cmp);
    24     do
    25     {
    26         for (int i = 0; i < n; i++) cout << f[i] << " ";
    27         //cout << endl;
    28         puts("");
    29     }while (prev_permutation(f, f + n)); 
    30     return 0;
    31 } 
  • 相关阅读:
    windows 1061
    Golang 编程思维和工程实战
    Apache Tomcat jar Catalina
    MySQL Client/Server Protocol mysql协议
    蚂蚁集团万级规模 k8s 集群 etcd 高可用建设之路
    实习生系列之找实习的途径
    Yahoo!网站性能最佳体验的34条黄金守则
    onselectstart="return false"无法复制文字
    VS2008开发环境中容易遇到的3个问题之解决办法
    实践与交流:“三保险”为世界顶级安全防范软件ESET Nod32 4.0的正常使用“保驾护航”
  • 原文地址:https://www.cnblogs.com/aiyi2000/p/9847845.html
Copyright © 2011-2022 走看看