Num 33: Function recursion [full arrangement]

Source: Internet
Author: User
Tags repetition



Mathematical full-Alignment questions:

Given the number of M, can be arranged into all cases of n-digit;


Example: 3 numbers (three-in-one) are arranged into two digits [containing duplicate numbers] are:

11,12, 13,21,22,23,31,32,33;

Example: 2 numbers are arranged in three digits:

111, 112, 121, 122, 211, 212, 221, 222;


From the above easy to find the law :


For the number of n-bit m requirements [may be set to 2 digits of thethree-to-one], we only need to start from the highest bit, followed by the first number (11);

Then keep the high position unchanged, the lowest bit travels all the numbers to be ranked: 11, 12, 13;

After the lowest bit traveled, the sub-lows became the second number to continue their travels: 21, 22, 23;

Know that the highest bit has traveled through all the numbers to be arranged;


Such a problem can be achieved through a for loop, but for n digits, it is necessary to travel through all bits of data with N for loops,

The code length is conceivable;

So we think of the recursive method to solve;

Problem Analysis:


1.

Because we cannot output the number of permutations at once by shaping it, so we have to put every bit of it into the array.

The need to define two arrays ( arr[M] and a[N] respectively represents the number of the number to be arranged and the first bit [I starting from 0];


2.

When I is less than n (i.e. when the requirement of n-digits is not reached), travel to the first position;

When I is greater than or equal to n (the requirement has been met) press I=0 to I=n-1 (first to last) output;


Implementation code (with duplicate numbers):

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>const int m=10;int n , M,a[m],arr[m]={1, 2, 3, 4};void dfs (int v) {    if (v >= N) {for        (int i = 0;i<n;i++)            printf ("%d", A[i]); 
   
    printf ("\ n");        return;    }    for (int i = 0; i<m;i++) {        a[v] = arr[i];        DFS (v+1);}    } int main () {while    (scanf ("%d%d", &n,&m) ==2) {        dfs (0);    } return 0;}
   

Code Analysis:


1.

DFS (0); Expression: Starting from the first (No. 0) number;


2.

arr[m]={1,2,3,4}: To achieve a full array of the number of M is 1, 2, 3, 4;


3.

When I is less than n (i.e. when the requirement of n-digits is not reached), travel to the first position;


for (int i = 0; i<m;i++) {        a[v] = arr[i];        DFS (v+1);    }

4.

When I is greater than or equal to n (the requirement has been met) press I=0 to I=n-1 (first to last) output;

  if (v >= N) {        for (int i = 0;i<n;i++)            printf ("%d", A[i]);        printf ("\ n");        return;    }


The above code is to implement a full array of repeating numbers , let's look at the full array without repeating numbers :


is basically the same as an algorithm with duplicate numbers, the only difference being:


a full permutation algorithm that does not contain duplicate numbers requires the introduction of an array of tokens (mark[m]);


Note that when M<n (the number of non-repeating permutations is less than the digits), this case is wrong and the result is not output;


Implementation code (with duplicate numbers):

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>const int m=10;int n , m,a[m],arr[m]={1,2,3,4},mark[m];void dfs (int v) {    if (v >= N) {        for (int i = 0;i<n;i++)            printf ("%d", a[ I]);        printf ("\ n");        return;    }    for (int i = 0; i<m;i++) {if (!mark[i]) {Mark[i]=1;a[v] = Arr[i];        DFS (v+1);        mark[i]=0;}}}    int main () {while    (scanf ("%d%d", &n,&m) ==2) {memset (mark,0,sizeof (Mark));        DFS (0);    } return 0;}

1.

Using backtracking: When I is less than n (i.e. when the requirement of n-digits is not reached), travel to the I-bit and add marker mark[i]=1;

When the mark[i]==1, the description has been used, the next number of permutations;

After the recursion is complete, it returns and will be re-marked as 0;

    for (int i = 0; i<m;i++) {if (!mark[i]) {Mark[i]=1;a[v] = Arr[i];        DFS (v+1);        mark[i]=0;}    }

2.

When I is greater than or equal to n (the requirement has been met) press I=0 to I=n-1 (first to last) output;

    if (v >= N) {        for (int i = 0;i<n;i++)            printf ("%d", A[i]);        printf ("\ n");        return;    }


Now that we know the whole permutation method of repetition and non-repetition, there is another problem:

What if you don't want to output a number that starts with a number (I bit)? (You can save time by excluding some cases before recursion)


Example: 3 numbers (three-to-one) are arranged in two digits [contain duplicate numbers] [ do not output numbers that begin with ]:

21,22,23,31,32,33;

Example: 3 numbers (three-to-one) are arranged in two digits [without repeating numbers] [ do not output numbers that begin with ]:

21,23,31,32;


In fact, just start by adding an array to determine whether the beginning (the first bit) is k;



①, the implementation code is as follows [contains duplicate numbers] [ does not output a number from the beginning ]:


#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>const int m=10;int n , M,a[m],arr[m]={1, 2, 3, 4};void dfs (int v) {    if (v >= N) {for        (int i = 0;i<n;i++)            printf ("%d", A[i]); 
   
    printf ("\ n");        return;    }    for (int i = 0; i<m;i++) {        a[v] = arr[i];        if (a[0]==1) continue;        DFS (v+1);}    } int main () {while    (scanf ("%d%d", &n,&m) ==2) {        dfs (0);    } return 0;}
   


Code Analysis:

only added judgment;


    for (int i = 0; i<m;i++) {        a[v] = arr[i];        if (a[0]==1) continue;        DFS (v+1);    }



②, the implementation code is as follows [does not contain duplicate numbers] [ does not output a number from the beginning ]:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>const int m=10;int n , m,a[m],arr[m]={1,2,3,4},mark[m];void dfs (int v) {    if (v >= N) {        for (int i = 0;i<n;i++)            printf ("%d", a[ I]);        printf ("\ n");        return;    }    for (int i = 0; i<m;i++) {if (!mark[i]) {Mark[i]=1;a[v] = arr[i];if (a[0]==1) {mark[i]=0;continue;}        DFS (v+1);        mark[i]=0;}}}    int main () {while    (scanf ("%d%d", &n,&m) ==2) {memset (mark,0,sizeof (Mark));        DFS (0);    } return 0;}

Code Analysis:

In backtracking, it is necessary to cancel the mark before jumping out (otherwise all of them will not be output);


    for (int i = 0; i<m;i++) {if (!mark[i]) {Mark[i]=1;a[v] = arr[i];if (a[0]==1) {mark[i]=0;continue;}        DFS (v+1);        mark[i]=0;}    }


Now basically you can do it all in a random order. ≥~~≤;


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Num 33: Function recursion [full arrangement]

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.