zoukankan      html  css  js  c++  java
  • 【全排序】next_permutation

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int ans[4]={1,2,3,4};
        sort(ans,ans+4);    /* 这个sort可以不用,因为{1,2,3,4}已经排好序*/
        do                             /*注意这步,如果是while循环,则需要提前输出*/
        {
            for(int i=0;i<4;++i)
                cout<<ans[i]<<" ";
            cout<<endl;
        }while(next_permutation(ans,ans+4));
        return 0;
    }
    输出
    1
    2 3 4 1 2 4 3 1 3 2 4 1 3 4 2 1 4 2 3 1 4 3 2 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 2 4 1 3 2 4 3 1 3 1 2 4 3 1 4 2 3 2 1 4 3 2 4 1 3 4 1 2 3 4 2 1 4 1 2 3 4 1 3 2 4 2 1 3 4 2 3 1 4 3 1 2 4 3 2 1

    拓展

    1.能否直接算出集合{1, 2, ..., m}的第n个排列?

    举例说明:如7个数的集合为{1, 2, 3, 4, 5, 6, 7},要求出第n=1654个排列。

    (1654 / 6!)取整得2,确定第1位为3(从0开始计数),剩下的6个数{1, 2, 4, 5, 6, 7},求第1654 % 6!=214个序列;

    (214 / 5!)取整得1,确定第2位为2,剩下5个数{1, 4, 5, 6, 7},求第214 % 5!=94个序列;

    (94 / 4!)取整得3,确定第3位为6,剩下4个数{1, 4, 5, 7},求第94 % 4!=22个序列;

    (22 / 3!)取整得3,确定第4位为7,剩下3个数{1, 4, 5},求第22 % 3!=4个序列;

    (4 / 2!)得2,确定第5为5,剩下2个数{1, 4};由于4 % 2!=0,故第6位和第7位为增序<1 4>;

    因此所有排列为:3267514。

    #include<bits/stdc++.h>
    using namespace std;
    
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        
            freopen("out.txt","w",stdout); 
         
        int ans[7]={1,2,3,4,5,6,7};
        sort(ans,ans+7);  /* 同上可以不用sort */
        int n=0; 
        do                             //注意这步,如果是while循环,则需要提前输出
        {
            if(n == 1654)
            {
                 for(int i=0;i<7;++i)
                cout<<ans[i];
                cout<<endl;
                break;
            }
            n++;
         }while(next_permutation(ans,ans+7));
        return 0;
    }

    2. 给定一种排列,如何算出这是第几个排列呢?

    和前一个问题的推导过程相反。例如3267514:

    后6位的全排列为6!,3为{1, 2, 3 ,4 , 5, 6, 7}中第2个元素(从0开始计数),故2*720=1440;

    后5位的全排列为5!,2为{1, 2, 4, 5, 6, 7}中第1个元素,故1*5!=120;

    后4位的全排列为4!,6为{1, 4, 5, 6, 7}中第3个元素,故3*4!=72;

    后3位的全排列为3!,7为{1, 4, 5, 7}中第3个元素,故3*3!=18;

    后2位的全排列为2!,5为{1, 4, 5}中第2个元素,故2*2!=4;

    最后2位为增序,因此计数0,求和得:1440+120+72+18+4=1654


    这个的代码实现,可以用一个数组a保存3267514,然后while调用next_permutation(),用n计数,每次与数组a比较,相等则输出n;

    #include<bits/stdc++.h>
    using namespace std;
    
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        
            freopen("out.txt","w",stdout); 
         
        int a[7]={3,2,6,7,5,1,4};
        int ans[7]={1,2,3,4,5,6,7};
        sort(ans,ans+7);  /* 同上可以不用sort */
        int n=0,flag=0;; 
        do                             //注意这步,如果是while循环,则需要提前输出
        {
            flag=0;
            for(int i=0;i<7;i++){
                if(a[i]!=ans[i]) flag=1;
            }
            if(!flag) break;
            n++;
         }while(next_permutation(ans,ans+7));
         cout<<n<<endl;
        return 0;
    }
  • 相关阅读:
    ios UIWebView截获html并修改便签内容(转载)
    IOS获取系统时间 NSDate
    ios 把毫秒值转换成日期 NSDate
    iOS  如何判断当前网络连接状态  网络是否正常  网络是否可用
    IOS开发 xcode报错之has been modified since the precompiled header was built
    iOS系统下 的手机屏幕尺寸 分辨率 及系统版本 总结
    iOS 切图使用 分辨率 使用 相关总结
    整合最优雅SSM框架:SpringMVC + Spring + MyBatis 基础
    Java面试之PO,VO,TO,QO,BO
    Notes模板说明
  • 原文地址:https://www.cnblogs.com/Kohinur/p/9027431.html
Copyright © 2011-2022 走看看