• buaaoj230——next_permutation的应用


    题目地址

    简单的全排列输出,借用stl中的next_permutation就非常简单了。

    关于next_permutation:(备忘,来源网络)

      1 /*这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
      2 与之完全相反的函数还有prev_permutation*/
      3   
      4   
      5 //(1) int 类型的next_permutation
      6   
      7 int main()
      8 {
      9  int a[3];
     10 a[0]=1;a[1]=2;a[2]=3;
     11  do
     12 {
     13 cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
     14 } while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度
     15   
     16 //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
     17   
     18   
     19 }
     20   
     21 输出:
     22   
     23  1 2 3
     24  1 3 2
     25  2 1 3
     26  2 3 1
     27  3 1 2
     28  3 2 1
     29   
     30   
     31 如果改成 while(next_permutation(a,a+2));
     32 则输出:
     33  1 2 3
     34  2 1 3
     35   
     36 只对前两个元素进行字典排序
     37 显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
     38   
     39   
     40   
     41 若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
     42   
     43  int list[3]={3,2,1};
     44 next_permutation(list,list+3);
     45 cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
     46   
     47 //输出: 1 2 3
     48  
     49   
     50   
     51   
     52   
     53 (2) char 类型的next_permutation
     54   
     55 int main()
     56 {
     57  char ch[205];
     58 cin >> ch;
     59   
     60 sort(ch, ch + strlen(ch) );
     61 //该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了
     62   
     63  char *first = ch;
     64  char *last = ch + strlen(ch);
     65   
     66  do {
     67 cout<< ch << endl;
     68 }while(next_permutation(first, last));
     69  return 0;
     70 }
     71   
     72 //这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
     73 //若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
     74 //若要整个字符串进行排序,参数5指的是数组的长度,不含结束符
     75  
     76   
     77   
     78   
     79   
     80   
     81 (3) string 类型的next_permutation
     82   
     83 int main()
     84 {
     85  string line;
     86  while(cin>>line&&line!="#")
     87 {
     88  if(next_permutation(line.begin(),line.end())) //从当前输入位置开始
     89 cout<<line<<endl;
     90  else cout<<"Nosuccesor
    ";
     91 }
     92 }
     93   
     94   
     95   
     96 int main()
     97 {
     98  string line;
     99  while(cin>>line&&line!="#")
    100 {
    101 sort(line.begin(),line.end());//全排列
    102 cout<<line<<endl;
    103  while(next_permutation(line.begin(),line.end()))
    104 cout<<line<<endl;
    105 }
    106 }
    107   
    108   
    109   
    110   
    111   
    112   
    113  next_permutation 自定义比较函数
    114   
    115   
    116 #include<iostream> //poj 1256 Anagram
    117 #include<string>
    118 #include<algorithm>
    119 using namespace std;
    120 int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
    121 {
    122  if(tolower(a)!=tolower(b))
    123  return tolower(a)<tolower(b);
    124  else
    125  return a<b;
    126 }
    127 int main()
    128 {
    129  char ch[20];
    130  int n;
    131 cin>>n;
    132  while(n--)
    133 {
    134 scanf("%s",ch);
    135 sort(ch,ch+strlen(ch),cmp);
    136  do
    137 {
    138 printf("%s
    ",ch);
    139 }while(next_permutation(ch,ch+strlen(ch),cmp));
    140 }
    141  return 0;
    142 }

    本题参考代码:

     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 int main()
     5 {
     6     int a[13],l,i;
     7     while(scanf("%d",&l)&&l!=0)
     8     {
     9         for(i=0;i<12;i++)
    10             a[i]=i+1;
    11     do{
    12         for(i=0;i<l;i++)
    13             printf("%d",a[i]);
    14         printf("
    ");
    15     }while (next_permutation(a, a + l));
    16     printf("
    ");
    17     }
    18 }
    /*这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
    与之完全相反的函数还有prev_permutation*/
      
      
    //(1) int 类型的next_permutation
      
    int 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,每执行一次,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_permutation
      
    int main()
    {
     string line;
     while(cin>>line&&line!="#")
    {
     if(next_permutation(line.begin(),line.end())) //从当前输入位置开始
    cout<<line<<endl;
     else cout<<"Nosuccesor ";
    }
    }
      
      
      
    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>
    using 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 ",ch);
    }while(next_permutation(ch,ch+strlen(ch),cmp));
    }
     return 0;
    }
  • 相关阅读:
    给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.4 -- 4.5
    给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.1--4.3
    因数表进阶:1--x的因数和
    【python】接口自动化初识第三站
    【python】接口自动化初识第二站
    【python】接口自动化初识第一站
    【pycharm】无法自动补齐代码
    【python】UI自动化框架搭建
    【jmeter】B接口通过正则表达式引用A接口的参数
    测试流程测试方法
  • 原文地址:https://www.cnblogs.com/quintessence/p/6115215.html
Copyright © 2020-2023  润新知