首页 > 其他 > 详细

ACM题目————STL + 全排列

时间:2016-05-17 17:40:00      阅读:255      评论:0      收藏:0      [点我收藏+]

今天碰到一个函数,感觉挺好用的,全排列函数

 

next_permutation! 他可以遍历全排列哦!

 

话不多说,直接上题。

 

描述

小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。

输入
第一行输入整数N(1<N<10)表示多少组测试数据,
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
输出
在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如
样例
样例输入
2
3 1
4 2
样例输出
1
2
3
12
13
14
21
23
24
31
32
34
41
42
43
//include <iostream>

using namespace std;

int main()
{
    int T;
    cin >> T ;
    while( T -- )
    {
        int n, m;
        string str, s;
        cin >> n >> m ;
        for(int i=1; i<=n; i++)
            str += ‘0‘ + i ;
        s = str.substr(0,m);//字符串剪切
        cout << s << endl ;
        while(next_permutation(str.begin(),str.end()))
        {
            if( s != str.substr(0,m))
            {
                s = str.substr(0,m);
                cout << s << endl ;
            }
        }
    }

    return 0;
}

 

ACM题目————STL + 全排列

原文:http://www.cnblogs.com/Asimple/p/5502311.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!