zoukankan      html  css  js  c++  java
  • next_permutation函数

    头文件:

      algorithm

    参数: 

      next_permutation(first,last)   

      next_permutation(first,last,cmp)

    first,last为两个iterator,分别指向目标的头和尾,cmp是一个bool函数,接受两个目标序列值,返回bool

    next_permutation函数每次返回0..1,并且如果可以,把目标序列变成下一个排列。

    我的样例代码:

     1 #include <iostream>
     2 #include <algorithm>
     3 
     4 using namespace std;
     5 
     6 const int Maxn=1000;
     7 
     8 bool cmp(int a,int b)
     9 {
    10     return a<b;
    11 }
    12 
    13 int main()
    14 {
    15     int dat[Maxn];
    16     int N=5;
    17 
    18     for (int i=1;i<=N;i++)
    19     {
    20         dat[i]=i;
    21     }
    22 
    23     do
    24     {
    25         for (int i=1;i<=N;i++)
    26         {
    27             cout<<dat[i]<<" ";
    28         }
    29         cout<<endl;
    30     }while (next_permutation(dat+1,dat+N+1,cmp));
    31 
    32 }

    关于效率问题...回头自己写一个DFS跟他的比较一下吧,感觉STL的实现应该做的还可以的。

  • 相关阅读:
    2013dgtcs 成绩排序
    JZOJ 1286. 太空电梯
    java单例模式Singleton
    设计模式的类型
    java工厂模式Factory
    Mysql SQL优化
    maven deploy命令打包到私服
    debian/linux 配置maven
    Java RC4加密解密工具
    JedisUtils
  • 原文地址:https://www.cnblogs.com/dandi/p/3949889.html
Copyright © 2011-2022 走看看