#include <stdio.h>#defineBR printf ("\ n");intans=0;Charel[502]; intConfirmintIintK//to compare back if the element has the same skip this condition{ if(i>k) { while(i>k) {if(El[i]==el[k])return 0; K++; } } return 1; }voidOpintN) {ans++; intJ; for(j=0; j<=n;j++) printf ("%c", El[j]); BR}voidPermintNintk)//N represents the number of ends of the current distance assignment (array pattern, -1) K represents the position of the current element{ intI,r; if(n==k) OP (n);//if two numbers have reached equal output Else for(i=k;i<=n;i++)//swap from the current array element to the end, one by one if(Confirm (I,k))//Exchange based on the returned results{R=el[i];el[i]=el[k];el[k]=R; Perm (N,k+1);//one of the advancing Kr=el[i];el[i]=el[k];el[k]=R; } }intMain () {intN,i; scanf ("%d",&N); GetChar (); for(i=0; i<n;i++) scanf ("%c", &el[i]);//the above is the normal input linkPerm (n1,0); printf ("%d", ans); return 0;}
Forgive me for my bad taste hahaha.
The whole idea is this:
A A C c
0 1 2 3
First exchange with yourself
a a C c
a a C c rear letter disorderly order
And then recursively go on, constantly nonalphanumeric each word back to the current position to find the next situation.
C a A C
If you're not swapping with yourself, check to see if you've swapped the same elements before. Like what:
A b c d e F e H
a b c e D f E H rear letter disorderly order
If the next and the next e exchange, then the effect and the first e the same, are in the following order regardless, ABCE down.
A b c D E F e h
a b c e E F D H
Test to find out from the back, so that the first person can be found
A b c D E F e H
K I
A b c D E F e H
K I is equal, skipping this condition
Slowly realize it. It should be clear.
Ka's retrospective programming exercise part6.5| | The permutation problem with repeating elements