HDOJ 1027 Ignatius and the Princess II

 

题意:给一个树组,长度为1,2,3。。。n ,求全排序里面第m小的序列。

思路:调用库函数next_permutation 水过

其中库中另一函数prev_permutation与next_permutation相反,由原排列得到字典序中上一次最近排列。

 

6939564 2012-10-18 11:10:31 Accepted 1027 31MS 260K 306 B C++ 罗维

 

View Code
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int f[1002];
 8     int n, m, i;
 9     while (cin>>n>>m)
10     {
11         for (i=0; i<n; i++)
12             f[i] = i+1;
13         m--;
14         while(m--)
15             next_permutation(f, f+n);
16         cout<<f[0];
17         for (i=1; i<n; i++)
18             cout<<" "<<f[i];
19         cout<<endl;
20     }
21 }

 

 

posted @ 2012-10-18 11:18  旅行的蜗牛  阅读(142)  评论(0编辑  收藏  举报