题目信息:给出n。m,求n个数的按字典序排列的第m个序列

http://acm.hdu.edu.cn/showproblem.php?

pid=1027

AC代码:

/**

 *全排列的个数(次序)

 */

#include<iostream>

#include<cstdio>

#include<algorithm>

int a[1001],x;

using namespace std;

void print(int n){

    for(int i=1;i<n;i++){

        cout<<a[i]<<" ";

    }

    cout<<a[n]<<endl;

}

void range(int k,int n,int m){//自定义的全排列函数(超时?

)

    if(k==n) {

        ++x;

        if(x==m){//控制全排列次数

            print(n);

            return;

        }

    }

    for(int i=k;i<=n;i++){

        swap(a[k],a[i]);

        range(k+1,n,m);

        swap(a[k],a[i]);

    }

}

int main()

{

    int n,m;

    while(scanf("%d%d",&n,&m)!=EOF){

        for(int i=1;i<=n;i++) a[i]=i;

        x=0;

        //range(1,n,m);

        for(int i=1;i<m;i++){//进行m-1次全排列

            next_permutation(a+1,a+n+1);//全排列库函数,从1開始,相当于迭代器

        }

        for(int i=1;i<n;i++){

            cout<<a[i]<<" ";

        }

        cout<<a[n]<<endl;

    }

    return 0;

}

最新文章

  1. 移动端IOS点击事件失效解决方案
  2. 微信小程序
  3. Linux下Source Insight的安装和汉化
  4. 51nod1039 x^3 mod p
  5. F2063 Could not compile used unit &#39;tt.pas&#39;
  6. mysql 乱码问题处理
  7. 从客户端(xxxxxxxxxxxxxxxxxxxxxx)中检测到有潜在危险的 Request.Form 值。
  8. 基于Spring MVC的简单HelloWorld实例
  9. IOS 通过button获取cell
  10. CI分支kohana在线文档
  11. cocos2d-x 3.1.1 学习笔记[17] 关于这些活动功能
  12. Elasticsearch-深入理解索引原理
  13. LeetCode第十七题-电话号码的字母组合
  14. 20165214 2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 Week5
  15. GitHub上最受欢迎的 5 大 Java 项目
  16. kafka-manager配置和使用
  17. PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
  18. 02-老马jQuery教程-jQuery事件处理
  19. SAP中方会计凭证打印解决方案
  20. 兼容主流浏览器的css渐变色

热门文章

  1. IPhone打包工具脚本
  2. TensorFlow和深度学习新手教程(TensorFlow and deep learning without a PhD)
  3. 【DB2】If &#39;db2&#39; is not a typo you can run the following command to lookup the package that contains the binary: command-not-found db2 bash: db2: command not found
  4. git 的简单用法
  5. 域对象的引用,ActionContext 和ServletActionContext类的使用
  6. R语言的if-else
  7. U盘装系统:安装GHOST Win7系统教程
  8. python导入模块的两种方式
  9. SICP 1.23-1.26体会
  10. Laravel建站03--建立前台文章列表和文章详情