HDU 1027 Ignatius and the Princess II (next_permutation)

题目

在这里插入图片描述在这里插入图片描述

代码

#include <bits/stdc++.h>
using namespace std;
int main ()
{
    
    
    int n,m;
    int num[1010];
    while (cin>>n>>m)
    {
    
    
        memset(num,0,sizeof(num));
        for (int i=0; i<n; i++)
            num[i]=i+1;
        for (int i=0; i<m-1; i++)
            next_permutation(num,num+n);
        for (int i=0; i<n-1; i++)
            cout<<num[i]<<" ";
        cout<<num[n-1]<<endl;
    }
    return 0;
}

理解

这里运用了 next_permutation(),及求下一个的排列组合。
该函数可以将数组(int char都可以)按照从小到大排列组合的顺序依次往下改变。

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

在这里即为第m小的排列顺序。之所以是m-1,应该是因为最初的排列顺序即为最小的顺序,若找第m小的,改变的次数不包括最初已有的顺序。

猜你喜欢

转载自blog.csdn.net/weixin_44918971/article/details/95620167
今日推荐