NYOJ 19 擅长排列的小明

来源:互联网 发布:三菱plc编程入门教程 编辑:程序博客网 时间:2024/05/20 10:54

擅长排列的小明

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。
输入
第一行输入整数N(1<N<10)表示多少组测试数据,
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
输出
在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例
样例输入
23 14 2
样例输出
123121314212324313234414243
来源
[hzyqazasdf]原创
上传者
hzyqazasdf

#include <iostream>#include <algorithm>#include <string>using namespace std;int main(){    int N,n,m;    cin>>N;    while(N--)    {        cin>>n>>m;        string str,s;//是字符串,每个元素都是char型!        for(int i = 1; i <= n; i++)        str += i+'0';        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;}


#include <iostream>#include <algorithm>#include <string>using namespace std;int main(){string a;int i;for( i = 1; i < 5; i++)    a += i+'0';    //cout<<a;do{for(i=0;i<4;i++)       cout<<a[i];       cout<<endl;}while(next_permutation(a.begin(),a.end()));return 0;} 


原创粉丝点击