您的位置:首页 > 其它

nyoj19(排列组合next_permutation(s.begin(),s.end()))

2014-11-02 20:14 232 查看
题目意思:

从n个数中选择m个数,按字典序输出其排列。

http://acm.nyist.net/JudgeOnline/problem.php?pid=19

例:

   输入:n=3,m=1; 输出:1 2 3

   输入:n=4,m=2; 输出:12 13 14 21 23 24 31 32 34 41 42 43

题目分析:

此题为全排列的前m个数,只需对n个数调用全排列函数next_permutation(),去除重复的输出前m个即可。

AC代码:

/**

 *改写的全排列,这里用字符串输入,方便判断是否重复输出

 */

#include<iostream>

#include<cstdio>

#include<algorithm>

#include<string>

using namespace std;

int n,m;

int main()

{

    int t;

    cin>>t;

    while(t--){

        cin>>n>>m;

        string s,s1;

        for(int i=1;i<=n;i++) s+=i+'0';

        s1=s.substr(0,m);

        cout<<s1<<endl;

        while(next_permutation(s.begin(),s.end())){

            if(s1!=s.substr(0,m)){//用来判断重复输出,否则会输出多次

                s1=s.substr(0,m);

                cout<<s1<<endl;

            }

        }

    }

    return 0;

}

       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: