Ka's retrospective programming exercise part6.5| | The permutation problem with repeating elements

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.