The combination of N output and N count in a full arrangement (number range: ~ B)

Source: Internet
Author: User

N output in full arrangement:

int WPermutation(int num, bool bRepeat)

Num indicates that num is in full order.

Whether the brepeat flag generates a sequence of repeated elements.

 

int Permutation(int n, int* A, int cur, bool bRepeat) {static int number = 0;if(cur == n){   number++;for(int i = 0; i< n; i++){printf("%d ", A[i]);}printf("\n");}else{for(int i = 1; i <= n; i++){int ok = 1;for(int j = 0; j < cur; j++)    {if(!bRepeat){if(A[j] == i){ok = 0;}}}if(ok){A[cur] = i;Permutation(n, A, cur + 1, bRepeat);}}}    return number;}int WPermutation(int num, bool bRepeat){printf("%d permutation(%s): %d ~ %d\n", num, bRepeat?"repeat mode":"single mode", 1, num);int n = num;int *A = (int*)malloc(n*sizeof(int));memset(A, 0, sizeof(n*sizeof(int)));int cur = 0;int number = Permutation(n, A, cur, bRepeat);delete [] A ;A = NULL;printf("over!\n");return number;}


int Wpermutation(int st, int en, int n, bool bRepeat);

Combination of N numbers (number range: St ~ ), Consider repeating elements:

N indicates a combination of N numbers.

The value range is St ~ En

Whether the brepeat flag generates a sequence of repeated elements.

 

int Permutation(int st, int en, int n, int* A, int cur, bool bRepeat){static int number = 0;if(cur == n){   number++;for(int i = 0; i< n; i++){printf("%d ", A[i]);}printf("\n");}else{for(int i = st; i <= en; i++){int ok = 1;for(int j = 0; j < cur; j++){if(!bRepeat){if(A[j] == i){ok = 0;}}}if(ok){A[cur] = i;Permutation(st, en, n, A, cur + 1, bRepeat);}}}return number;}int Wpermutation(int st, int en, int n, bool bRepeat){printf("%d permutation(%s): %d ~ %d\n", n, bRepeat?"repeat mode":"single mode", st, en);int num = en - st + 1;if(n > num){bRepeat = true;printf("too many number, to be repeat mode:\n");}int *A = (int*)malloc(n*sizeof(int));memset(A, 0, sizeof(n*sizeof(int)));int cur = 0;int number = Permutation(st, en, n, A, cur, bRepeat);delete [] A ;A = NULL;printf("over!\n");return number;}

The combination of N output and N count in a full arrangement (number range: ~ B)

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.