Full arrangement (traditional && black technology)

Some of the violence scores of the recent exams have been used in full ranking.

All in all is a good thing...

Think back, when did we first learn about full permutations?

Maybe it's time to learn to search...

1. Traditional search algorithm

If you want to review, you can poke https://www.luogu.org/problemnew/show/P1706

 

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstdio>
 4 #include<iomanip> 
 5 using namespace std;
 6 int num=0,a[15]={0},n,r;
 7 bool b[15]={0};
 8 int search(int);
 9 int print();
10 int main(){
11     cin>>n;
12     search(1);
13     cout<<num<<endl;
14     system("pause");
15     return 0;
16     }
17 int search(int k)//k是找第几位数 
18 {  for(int i=1;i<=n;i++)
19    if(!b[i])
20    {  a[k]=i;
21       b[i]=1;
22       if(k==n)print();
23        else search(k+1);
24          b[i]=0;}
25 }
26 int print()
27   { num++;
28     for(int i=1;i<=n;i++)
29     cout<<a[i];
30     cout<<endl;
31 }
View Code

 

Second, use the universal STL <algorithm> template library

A function: next_permutation()

The code is:

 1 #include<algorithm>
 2 #include<cstdio>
 3 using namespace std;
 4 int a[8]={1,2,3,4,5,6,7};
 5 int n;
 6 int main()
 7 {
 8     scanf("%d",&n);
 9     do{
10         for(int i=0;i<n;i++)
11         {
12             printf("%d\0",a[i]);
13         }
14         printf("\n");
15     }while(next_permutation(a,a+n));
16     return 0;
17 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325338239&siteId=291194637