-
输入
-
第一行输入整数N(1<N<10)表示多少组测试数据,
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
输出
- 在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。
(题目为ACM题目)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(void)
{
int N,n,m; //定义题目要求的三个变量 N,n,m
string str,str1,temp_str;//定义 用来保存字符串的变量。
cin>>N;
while( (N--)> 0) //控制循环次数
{
str =""; //重置str
cin>>n>>m;
for(int i =1 ; i <=n ; i++)
str += char('0'+i); //将1-n数字保存到str中
str1.assign(str,0,m); //从str中下标为0开始,取出m个,并保存到str1中。
cout<<str1<<endl;
while(next_permutation(str.begin(),str.end())) //使用C++算法中的next_permutation函数对str进行全排列
{
temp_str.assign(str,0,m); // 取出子串保存到temp_str中
if(str1!=temp_str) //判断当前子串是否刚刚出现过,如果出现则跳过。
{
str1 = temp_str;
cout<<str1<<endl;
}
}
}
return 0;
}