hdoj 1027 Ignatius and the Princess II

http://acm.hdu.edu.cn/showproblem.php?pid=1027
参考博客:http://blog.csdn.net/ac_gibson/article/details/45308645
用STL中的next_permutation()函数,用法参见上面的博客,得到按字典序排列的第m大的n的全排列。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int n,m,num,f[1010];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=1010;i++)
            f[i]=i;
        num=1;
        do{
            if(num==m) break;
            num++;
        }while(next_permutation(f+1,f+1+n));
        for(int i=1;i<=n-1;i++)
        printf("%d ",f[i]);
        printf("%d\n",f[n]);
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/sdxtcqs/article/details/78767462