Xiaoming who is good at arranging
Time Limit:
1000
ms | Memory Limit:
65535
KB
Difficulty:
4
- describe
- Xiao Ming is very smart and very good at permutation calculations. For example, if you give Xiao Ming a number 5, he can immediately give the full arrangement of 1-5 in lexicographical order. If you want to embarrass him, choose a few numbers from these 5 numbers and let him continue to arrange the full arrangement, then you are wrong. , he is equally good at it. Now you need to write a program to verify whether Xiao Ming, who is good at permutation, is right.
- enter
-
The first line of input integer N (1<N<10) indicates how many sets of test data,
each set of test data has two integers nm in the first line (1<n<9,0<m<=n) - output
- Select m characters from 1-n for full arrangement, and output them all in lexicographical order, each arrangement occupies one line, and there is no need to divide each group of data. as example
- sample input
-
2 3 1 4 2
- Sample output
-
1 2 3 12 13 14 21 23 24 31 32 34 41 42 43
- code show as below:
#include<cstdio>
using namespace std;
using namespace std;
const int maxn=101;
int p[maxn];
int n,m;
bool hashTable[maxn]={false};
int p[maxn];
int n,m;
bool hashTable[maxn]={false};
void DFS(int index){
if(index>=m+1){
for(int i=1;i<=m;i++)
printf("%d",p[i]);
printf("\n");
return;
}
for(int i=1;i<=n;i++){
if(hashTable[i]==false){
p[index]=i;
hashTable[i]=true;
DFS(index+1);
hashTable[i]=false;
}
}
}
int main()
{
int N;
scanf("%d",&N);
while(N--){
scanf("%d %d",&n,&m);
DFS(1);
}
return 0;
}
if(index>=m+1){
for(int i=1;i<=m;i++)
printf("%d",p[i]);
printf("\n");
return;
}
for(int i=1;i<=n;i++){
if(hashTable[i]==false){
p[index]=i;
hashTable[i]=true;
DFS(index+1);
hashTable[i]=false;
}
}
}
int main()
{
int N;
scanf("%d",&N);
while(N--){
scanf("%d %d",&n,&m);
DFS(1);
}
return 0;
}