csdnceshi58
Didn"t forge
采纳率100%
2018-11-02 02:10

在 c 语言中将嵌套的 for 循环转换为递归

  • c
已采纳
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
          //  0  1  2  3  4  5  6  7
int arr[] = { 3, 6, 1, 2, 6, 7, 8, 4};
int bee[] = { 6, 8, 1, 4, 2, 6, 3, 7};
int i = 0;
int j = 0;
int matches[120] = {0};
int length1 = 8;

void find_matches(int *arr, int *bee, int*matches);

void find_matches(int *arr, int *bee, int *matches)
{
    for (i = 0; i<length1; i++)
    {
        for (j = 0; j < length1; j++)
        {
            if (arr[i]==bee[j])
            {
                matches[i] = j;
            }
        }
    }

    for (int z = 0; z<8; z++)
    {
        printf("%d\n", matches[z]);
    }
}

int main()
{
    find_matches(arr, bee, matches);
}

The gist of my code is that it is matches every value of arr[] to bee[] and puts the index of the matches as numbers in the matches arrays and prints.

For example the value 3 in arr[0] is matched to the value 3 in bee[5] so the value of matches[0] will be 5.

How can I turn this into a recursive function?

I tried keeping the outer for loop and running the outer with a recursion function call inside but I don't know how to set up the variables and such.

转载于:https://stackoverflow.com/questions/53111823/convert-a-nested-for-loop-into-recursion-in-c

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • csdnceshi67 bug^君 3年前

    Twofold recursion, over both arrays - see the comments:

    // recursives
    void find_matches(int *arr, int *bee, int *matches, int current_position_in_arr);
    void find_matches_in_bee(int *arr, int *bee, int *matches, int current_position_in_arr, int current_position_in_bee);
    
    // wrapper
    void find_matches(int *arr, int *bee, int *matches) {
        find_matches(arr, bee, matches, 0);
    }
    
    // outer loop : iterate over 'arr'
    void find_matches(int *arr, int *bee, int *matches, int current_position_in_arr) {
        // check where arr[current_position_in_arr] is present in bee
        find_matches_in_bee(arr, bee, matches, current_position_in_arr, 0);
    
        // "next iteration of loop" - we check the next element in arr
        if (current_position_in_arr + 1 < length) {
            find_matches(arr, bee, matches, current_position_in_arr + 1);
        }
    }
    
    // inner loop : iterate over 'bee'
    void find_matches_in_bee(int *arr, int *bee, int *matches, int current_position_in_arr, int current_position_in_bee) {
        // do your business magic
        if (arr[current_position_in_arr] == bee[current_position_in_bee]) {
           ....
        }
    
        // "next iteration of loop" - we check the next element in bee
        if (current_position_in_bee + 1 < length) {
            find_matches_in_bee(arr, bee, matches, current_position_in_arr, current_position_in_bee + 1);
        }
    }
    

    Invoke the same way as before:

    find_matches(arr, bee, matches);
    

    The lesson here is that you can replace stuff like:

    int *array;
    
    for (int i = 0; i < LEN; ++i) {
      f(array[i]);
    }
    

    with

    void f(int *array) {
      f_inner(array, 0);
    }
    
    void f_inner(int *array, int position) {
      // business logic on array[position]
    
      // iteration step
      if (position + 1 < LEN) {
        f_inner(array, position + 1);
      }
    }
    
    点赞 评论 复制链接分享
  • csdnceshi68 local-host 3年前

    Your code is clean and clear as presented in nested loops. Why would you want to delibrately convert it to recursion? If you really want, how about

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
              //  0  1  2  3  4  5  6  7
    int arr[] = { 3, 6, 1, 2, 6, 7, 8, 4};
    int bee[] = { 6, 8, 1, 4, 2, 6, 3, 7};
    int i = 0;
    int j = 0;
    int matches[120] = {0};
    int length1 = 8;
    
    // iterate over bee
    void match(int i, int j)
    {
        if (j < 0)
            return;
        if (arr[i] == bee[j]) {
            matches[i] = j;
            return;
        }
        match(i, j - 1);
    }
    
    // iterate over arr
    void fit(int i)
    {
        if (i > 7)
            return;
        match(i, 7);
        fit(i + 1);
    }
    
    void find_matches_recursively()
    {
        fit(0);
        for (int z = 0; z < 8; z++)
            printf("%d\n", matches[z]);        
    }
    
    int main()
    {
        find_matches_recursively();
    }
    

    Output

    6
    5
    2
    4
    5
    7
    1
    3
    

    which is the same as in your code.

    Note: You have duplicated values in your bee[8] (value 6 in position 0 and position 5). Your iteration loops over all values and records the last match. My recursion searches from the tail and returns immediately when it finds the first match. The effect is the same.

    点赞 评论 复制链接分享
  • csdnceshi76 斗士狗 3年前

    like this :

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
              //  0  1  2  3  4  5  6  7
    int arr[] = { 3, 6, 1, 2, 6, 7, 8, 4};
    int bee[] = { 6, 8, 1, 4, 2, 6, 3, 7};
    int matches[120] = {0};
    int length1 = 8;
    
    void find_matches(int *arr, int *bee, int *matches, int i)
    {
        if (i == length1)
            return ;
        for (int j = 0; j < length1; ++j)
            if (arr[i]==bee[j])
                matches[i] = j;
        printf("%d\n", matches[i]);
        find_matches(arr, bee, matches, i + 1);
    }
    
    int main()
    {
        find_matches(arr, bee, matches, 0);
        return 0;
    }
    
    点赞 评论 复制链接分享