HDU 1027 Ignatius and the Princess II (next_permutation)

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


题目大意: 给你一个N个元素的序列为1,2,3……n-1,n组成一个排列。 问第M大的排列顺序为多少


解题思路: 直接用STL中的next_permutation


next_permutation用法: next_permutation(num, num+n);


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1111;
int num[maxn];
int main ()
{
    int n, m;
    while(scanf("%d %d", &n, &m) != EOF)
    {
        for(int i = 0; i < n; i++)
            num[i] = i+1;

        for(int i = 1; i < m; i++)
            next_permutation(num, num+n);

        for(int i = 0; i < n; i++)
        {
            if(i == n-1) printf("%d\n", num[i]);
            else printf("%d ", num[i]);
        }

    }
    return 0;
}


你可能感兴趣的:(STL)