欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 1027 [Ignatius and the Princess II]STL全排列

程序员文章站 2022-03-01 23:20:57
...

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1027

题目大意:求1-N的数列的第M个有序全排列

关键思想:用STL的next_permutation函数。或者康托展开

代码如下:

//STL全排列应用 
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	int n,m;
	int a[1010];
	while(cin>>n>>m){
		for(int i=0;i<1010;i++)a[i]=i;
		while(--m&&next_permutation(a+1, a+n+1));
		for (int i = 1 ;i < n;i ++ ) cout << a[i] << " " ;
		cout <<a[n]<< endl;
	}
	return 0;
}