zoukankan      html  css  js  c++  java
  • 老子的全排列呢 (STL--全排列)

    实质是使用C++库中自带的函数next_permutation(),括号内是需要排列的数组下标(begin,end),需要声明头文件<algorithm>

    以 牛客竞赛 老子的全排列呢  为例题

    使用方法如下(代码):

    这个题是输出1-8的全排列

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int a[8]={1,2,3,4,5,6,7,8};
        do//一个循环 ,内容为对当前排列进行的操作 
        {
            for(int i=0;i<8;i++)
            {
                printf("%d",a[i]);
                if(i!=7)
                printf(" ");
            }
            printf("
    ");
        }
        while(next_permutation(a,a+8));//while内放对应函数 
        return 0;
    }

     题  hdu1027  Ignatius and the Princess II  

    是要求输出1-n的第m个排列

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
        int a[1010];
        for(int i=1;i<=n;i++)
        {
            a[i]=i;
        }
        sort(a+1,a+n+1);//排序,从第一个字典序开始排列 
        int num=1;//第一个字典序的排列标记为第一个 
        do//当不是第m个排列时继续调用next_permutation()函数进行重排 
        {
            if(num==m)
            {
                for(int i=1;i<=n;i++)
                {
                    if(i!=1)
                    {
                        printf(" ");
                    }
                    printf("%d",a[i]);
                }
                break;
            }
            num++;//不符合则计数 
        }
        while(next_permutation(a+1,a+n+1));
        cout<<endl;
    }
        return 0;
    }
  • 相关阅读:
    C++ 内置函数 判断字母、数字及大小写转换
    C++11 随机数 random
    rpc
    C++11 智能指针
    xargs 命令使用
    记录优秀的文章
    腾讯 测试开发
    struts2文件上传、下载、防止重复提交
    注解
    @RestController注解
  • 原文地址:https://www.cnblogs.com/theshorekind/p/14317009.html
Copyright © 2011-2022 走看看