首页 > 其他 > 详细

杭电ACM1027——Ignatius and the Princess II

时间:2015-05-04 15:34:30      阅读:197      评论:0      收藏:0      [点我收藏+]

这题,找了好久,都没有找到是什么规律,百度了之后,才知道是第几个最小的排列是枚举排列的第几个。

真是长知识了!!~


知道了这样的规律之后,就可以很快的写出了,algorithm这个头文件中又一个枚举排列的函数next_permutation,第i个最小的排列,就是调用next_permutation  i - 1次之后的那个排列。next_permutation同样的适用与可重集,也就是集合中存在重复元素。


下面是AC的代码:

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n, m, p[1005];
	while(cin >> n >> m)
	{
		for(int i = 0; i < n; i++)
			p[i] = i + 1;
		m--;
		while(m--)
		{
			next_permutation(p, p + n);
		}
		for(int j = 0; j < n; j++)
			j == n - 1 ? printf("%d\n", p[j]) : printf("%d ", p[j]);
	}
	return 0;
}


杭电ACM1027——Ignatius and the Princess II

原文:http://blog.csdn.net/qq_25425023/article/details/45480907

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!