zoukankan      html  css  js  c++  java
  • 整形数组与字符串(字符数组)nex_permutation(或者是prve_permutation)的区别

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int main()
    {
     int i,a[3]={2,1,3};
     //sort(a,a+3);//没有这个的话,只能从当前排列。
     for(int j=0;j<6;j++)
     {
      next_permutation(a, a+3);//可以循环,即超过最大排列后又从最小排列。
         for(i=0;i<3;i++)
          printf("%d ",a[i]);
      printf("\n");
     }
     return 0;
    }

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int main()
    {
        int l,i;
     char a[15];
     scanf("%s",a);
     l=strlen(a);
     //sort(a,a+l,);//没有这个的话,只能从当前排列。
     printf("%s\n",a);//必须要把第一个输出
     for(i=0;i<7;i++)
     {
      next_permutation(a, a+l);//可以循环
      printf("%s\n",a);
     }
     return 0;
    }

     

    这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>

    下面是以前的笔记

    (1)    int 类型的next_permutationint main()

    {

        int a[3];

        a[0]=1;a[1]=2;a[2]=3;

        do 

        {

            cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;

        } while (next_permutation(a,a+3));        //参数3指的是要进行排列的长度//如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false(用!=0或者!=NULL判断是否为最后一个),每执行一次,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

    (2)    char 类型的next_permutation int main(){

         char ch[205];

         cin >> ch;

         sort(ch, ch + strlen(ch) );//该语句对输入的数组进行字典升序排序。如输入9874563102    cout<<ch;     将输出0123456789,这样就能输出全排列了

         char *first =  ch;

         char *last = ch + strlen(ch);

         do {

                   cout << ch << endl;

             }while(next_permutation(first, last));

         return 0;

    }//这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序//若采用        while(next_permutation(ch,ch+5));    如果只输入1562,就会产生错误,因为ch中第五个元素指向未知//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符

    (3)    string 类型的next_permutationint main()

    {

    string line;

    while(cin>>line&&line!="#")

    {

      if(next_permutation(line.begin(),line.end()))                    //从当前输入位置开始       cout<<line<<endl;

            else cout<<"Nosuccesor\n";

        }

    }int main()

    {

        string line;

        while(cin>>line&&line!="#")

        {

            sort(line.begin(),line.end());//全排列        cout<<line<<endl;

     while(next_permutation(line.begin(),line.end()))

                cout<<line<<endl;       

        }

    }

    next_permutation 自定义比较函数

    #include<iostream>        //poj 1256 Anagram#include<string>

    #include<algorithm>u

    sing namespace std;

    int cmp(char a,char b)     //'A'<'a'<'B'<'b'<...<'Z'<'z'.

    {

        if(tolower(a)!=tolower(b))/*将小写字母转换为大写字母,若是大写则不转换*/

            return tolower(a)<tolower(b);

        else

            return a<b;

    }int main()

    {

        char ch[20];

        int n;

        cin>>n;

        while(n--)

        {

            scanf("%s",ch);

            sort(ch,ch+strlen(ch),cmp);

            Do//先做一次否则输不出第一个

            {

                printf("%s\n",ch);

            }while(next_permutation(ch,ch+strlen(ch),cmp));    

        }

       return 0;

    }

     

  • 相关阅读:
    阿里巴巴校招内推简历筛选方案
    SIFT中的尺度空间和传统图像金字塔
    boost的编译
    H264与RTP
    link2001错误无法解析外部符号metaObject
    windows 7下qtcreator里QWT文件的pro配置
    电脑键盘上你所不知道的秘密,学会了很牛气!
    http://blog.csdn.net/chenriwei2/article/details/38047119
    Seaborn中的kdeplot、rugplot、distplot与jointplot
    8-Pandas扩展之Pandas提升性能的方法(eval()、query())
  • 原文地址:https://www.cnblogs.com/heqinghui/p/2606598.html
Copyright © 2011-2022 走看看