zoukankan      html  css  js  c++  java
  • 全排列 递归实现

    前面我们介绍了全排列的非递归算法,现在我再来写一下全排列的递归算法:

    这两种算法的算法思路并不相同。递归算法的思路比较接近于我们现实生活中的思路。

    1.试想,我们只有两个数字:12.要对它进行全排列,第一种方式就是12本身,第二种,将12交换,变为21即可。这提示了我们一种交换的思路。

    2.但这概括的并不全面。试想,我们要对123进行全排列。我们可以采用将1固定,“23”进行全排列,将“2”固定,对“13”进行全排列。将“3”固定,对“12”进行全排列。

    这其实就是首部为”1“,然后是“2”,然后是“3”,不就是第二位后边的数依次和第一位进行交换么?这是典型的递归的思路。

    3.但是,这样也不全面,我们每次交换要将排列恢复成为原始的“123”,因为这个算法求排列的时候,前后并没有依赖性,其参考物只有“123”这个原始的第一个排列。否则,如果我们不恢复的话,就会出现,虽然数量与正确解法相同,但是会有重复的排列的现象。

    这样,我们不难写出代码:

    [cpp] view plain copy
     
    1. #include <iostream>  
    2.   
    3. using namespace std;  
    4. int total = 0;  
    5. //交换函数  
    6. void swapArray(int &a,int &b)  
    7. {  
    8.     int temp;  
    9.     temp = a;  
    10.     a = b;  
    11.     b = temp;  
    12. }  
    13. //递归函数  
    14. void fullPermutation(int * fullArray,int start,int end,int number){  
    15.     //这里,既可以是">=",也可以是">",,应该也可以是"=="  
    16.     if(start>=end){  
    17.         for(int i=0;i<number;i++){  
    18.             cout<<fullArray[i];  
    19.         }  
    20.         cout<<endl;  
    21.         total++;  
    22.     }  
    23.     else{  
    24.         for(int i=start;i<=end;i++){  
    25.             swapArray(fullArray[start],fullArray[i]);//交换  
    26.             fullPermutation(fullArray,start+1,end,number);  
    27.             swapArray(fullArray[start],fullArray[i]);//注意恢复原样  
    28.         }  
    29.     }  
    30. }  
    31. int main()  
    32. {  
    33.     int number;//全排列的长度  
    34.     cout<<"Number:"<<endl;  
    35.     cin>>number;  
    36.     int * fullArray = new int[number];//动态生成全排列的数组  
    37.     //初始化  
    38.     for (int i=0;i<number;i++)  
    39.     {  
    40.         fullArray[i] = i+1;  
    41.     }  
    42.     fullPermutation(fullArray,0,number-1,number);  
    43.     cout<<"Total = "<<total;  
    44.     return 0;  
    45. }  

    全排列在很多程序都有应用,是一个很常见的算法,常规的算法是一种递归的算法,这种算法的得到基于以下的分析思路。  给定一个具有n个元素的集合(n>=1),要求输出这个集合中元素的所有可能的排列。

            一、递归实现

            例如,如果集合是{a,b,c},那么这个集合中元素的所有排列是{(a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a)},显然,给定n个元素共有n!种不同的排列,如果给定集合是{a,b,c,d},可以用下面给出的简单算法产生其所有排列,即集合(a,b,c,d)的所有排列有下面的排列组成:

         (1)以a开头后面跟着(b,c,d)的排列

        (2)以b开头后面跟着(a,c,d)的排列

        (3)以c开头后面跟着(a,b,d)的排列

        (4)以d开头后面跟着(a,b,c)的排列,这显然是一种递归的思路,于是我们得到了以下的实现:

    [cpp] view plain copy
     
    1. #include "iostream"  
    2. using namespace std;  
    3.   
    4. void permutation(char* a,int k,int m)  
    5. {  
    6.     int i,j;  
    7.     if(k == m)  
    8.     {  
    9.         for(i=0;i<=m;i++)  
    10.             cout<<a[i];  
    11.         cout<<endl;  
    12.     }  
    13.     else  
    14.     {  
    15.         for(j=k;j<=m;j++)  
    16.         {  
    17.             swap(a[j],a[k]);  
    18.             permutation(a,k+1,m);  
    19.             swap(a[j],a[k]);  
    20.         }  
    21.     }  
    22. }  
    23. int main(void)  
    24. {  
    25.     char a[] = "abc";  
    26.     cout<<a<<"所有全排列的结果为:"<<endl;  
    27.     permutation(a,0,2);  
    28.     system("pause");  
    29.     return 0;  
    30. }  

          二、STL实现

            有时候递归的效率使得我们不得不考虑除此之外的其他实现,很多把递归算法转换到非递归形式的算法是比较难的,这个时候我们不要忘记了标准模板库已经实现的那些算法,这让我们非常轻松。STL有一个函数next_permutation(),它的作用是如果对于一个序列,存在按照字典排序后这个排列的下一个排列,那么就返回true且产生这个排列,否则返回false。注意,为了产生全排列,这个序列要是有序的,也就是说要调用一次sort。实现很简单,我们看一下代码:

    [cpp] view plain copy
     
    1. #include "iostream"  
    2. #include "algorithm"  
    3. using namespace std;  
    4.   
    5. void permutation(char* str,int length)  
    6. {  
    7.     sort(str,str+length);  
    8.     do  
    9.     {  
    10.         for(int i=0;i<length;i++)  
    11.             cout<<str[i];  
    12.         cout<<endl;  
    13.     }while(next_permutation(str,str+length));  
    14.   
    15. }  
    16. int main(void)  
    17. {  
    18.     char str[] = "acb";  
    19.     cout<<str<<"所有全排列的结果为:"<<endl;  
    20.     permutation(str,3);  
    21.     system("pause");  
    22.     return 0;  
    23. }  

              三、有一定约束条件的全排列

             对数1,2,3,4,5要实现全排序。要求4必须在3的左边,其它的数位置随意。 

                思路:首先使用上面的2种方法之一实现全排列,然后对全排列进行筛选,筛选出4在3左边的排列。

    [cpp] view plain copy
     
    1. #include "iostream"  
    2. #include "algorithm"  
    3. using namespace std;  
    4.   
    5. void permutation(int* a,int length)  
    6. {  
    7.     int i,flag;  
    8.     sort(a,a+length);  
    9.     do  
    10.     {  
    11.         for(i=0;i<length;i++)  
    12.         {  
    13.             if(a[i]==3)  
    14.                 flag=1;  
    15.             else if(a[i]==4)             //如果3在4的左边,执行完代码,flag就是2  
    16.                 flag=2;  
    17.         }  
    18.         if(flag==1)          //如果4在3的左边,执行完代码,flag就是1  
    19.         {  
    20.             for(i=0;i<length;i++)  
    21.                 cout<<a[i];  
    22.             cout<<endl;  
    23.         }  
    24.     }while(next_permutation(a,a+length));  
    25.   
    26. }  
    27. int main(void)  
    28. {  
    29.     int i,a[5];  
    30.     for(i=0;i<5;i++)  
    31.         a[i]=i+1;  
    32.     printf("%d以内所有4在3左边的全排列结果为: ",i);  
    33.     permutation(a,5);  
    34.     system("pause");  
    35.     return 0;  
    36. }  

    (转)

  • 相关阅读:
    [SSRS] Use Enum values in filter expressions Dynamics 365 Finance and Operation
    Power shell deploy all SSRS report d365 FO
    display method in Dynamics 365 FO
    How To Debug Dynamics 365 Finance and Operation
    Computed columns and virtual fields in data entities Dynamics 365
    Azure DevOps for Power Platform Build Pipeline
    Create readonly entities that expose financial dimensions Dynamics 365
    Dataentity call stack dynamics 365
    Dynamics 365 FO extension
    Use singletenant servertoserver authentication PowerApps
  • 原文地址:https://www.cnblogs.com/qiaoyanlin/p/6744911.html
Copyright © 2011-2022 走看看