【问题标题】:Recursion in C - Largest element of an arrayC中的递归 - 数组的最大元素
【发布时间】:2020-03-18 05:25:44
【问题描述】:

此程序使用递归方法返回数组的最大元素:

#include <stdio.h>

int maiorVetor(int *V, int N) {
    int maior;
    if (N == 1)
        return V[0];
    else {
        maior = maiorVetor(V, N - 1);
        if (maior > V[N - 1])
            return maior;
        else
            return V[N - 1];
    }
}

int main () {
    int A[10] = { 12, 65, 14, 38, 33, 11, 20, 23, 21, 43 };

    printf("%d", maiorVetor(A, 10));
    return 0;
}

有人可以逐步解释一下这个程序是如何工作的吗?因为在我看来,当程序到达maior = maiorVetor(V, N-1); 时,它似乎不会继续,而是重新启动,直到N = 0。但实际上程序可以工作,它确实给了我一个真正的解决方案。我很困惑这种递归如何工作。

【问题讨论】:

  • 你认为当它到达maior = maiorVetor(V, N-1) 时会发生什么?它调用maiorVetor,对吗?当maiorVetor 返回时,会发生什么?它进入下一行,对吗? if(maior &gt; V[N-1])
  • 只使用一个由 3 个整数组成的数组,可以相对容易地在纸上或头脑中计算出来。使用调试器单步调试代码,看看会发生什么。
  • 尝试将 maior 初始化为零:maior =0
  • 了解了程序的工作原理后,就可以解释程序的工作原理了。稍微解释一下更常见的“要理解递归,你必须先理解递归。”
  • 但是当 maiorVetor 返回时数组只是 V[0] 因为它已经多次递减 N 对吗?而当它进入下一行时,数组有 N=0 还是没有?

标签: c arrays recursion


【解决方案1】:

我将画一棵递归树,然后尝试解释它

我正在修改和减小程序中数组的大小,以便于解释 -

#include <stdio.h>
int maiorVetor(int A, int N) {
    int maior;
    if(N == 1)
        return A[0];
    else {
        maior = maiorVetor(A, N-1);
        if(maior > A[N-1])
            return maior;
        else
            return A[N-1];
    }
}
int main () {
    int A[4] = {12, 65, 14, 38};

    printf("%d", maiorVetor(A, 4));

return 0;

}

据我所知,这就是本计划中发生的事情->


说明-(参考递归树图)

步骤1)(参考递归树图)

int maiorVetor(int A, int 4) {
    int maior;
    if(N == 1) // here 4!=1
        return A[0];
    else {
        maior = maiorVetor(A, N-1); //this is where recursion happens.

                                    //So,maior=maiorVector(A,4-1)=maiorVector(A,3)

                                    //Now here, we again call the maiorVector function 
                                    //with value maiorVector(A,3)

        /*So, for now we leave original function func. maiorVector(A,4) just at this 
        line, i.e , just before the if statement. 
        We'll touch this if statement and consequent statements again in Step 8.*/  

       if(maior > A[N-1]) /*LEFT FOR 
            return maior;   STEP 8*/
        else
            return A[N-1]; /*LEFT FOR STEP 8*/ 
    }
}

第二步)(参考递归树图)

int maiorVetor(int A, int 3) {
    int maior;
    if(N == 1) // here 3!=1
        return A[0];
    else {
        maior = maiorVetor(A, N-1); //this is where recursion happens.

                                    //So,maior=maiorVector(A,3-1)=maiorVector(A,2)

                                    //Now here, we again call the maiorVector function 
                                    //with value maiorVector(A,2)

        /*So, for now we leave original function func. maiorVector(A,3) just at this 
        line, i.e , just before the if statement.
        We'll touch this if statement and consequent statements again in Step 7.*/  

       if(maior > A[N-1])
            return maior;
        else
            return A[N-1];
    }
}

第三步)(参考递归树图)

int maiorVetor(int A, int 2) {
    int maior;
    if(N == 1) // here 2!=1
        return A[0];
    else {
        maior = maiorVetor(A, N-1); //this is where recursion happens.

                                    //So,maior=maiorVector(A,2-1)=maiorVector(A,1)

                                    //Now here, we again call the maiorVector function 
                                    //with value maiorVector(A,1)

        /*So, for now we leave original function func. maiorVector(A,2) just at this 
        line, i.e , just before the if statement.
        We'll touch this if statement and consequent statements again in Step 6.*/  

       if(maior > A[N-1])
            return maior;
        else
            return A[N-1];
    }
}

第四步)(参考递归树图)

int maiorVetor(int A, int 1) {
    int maior;
    if(N == 1) // YES,HERE 1==1.SO WE REUTRN A[0]=12.
       return A[0]; //THE LOOP IS TERMINATED AND WE EXIT FROM mainVector(int A,int 1).
    else {
        maior = maiorVetor(A, N-1);           
       if(maior > A[N-1])
            return maior;
        else
            return A[N-1];
    }
}

第五步)(参考递归树图)

/*From this step, we start coming out of the recursion. That, according to the 
Recursive tree diagram, we start moving in the reverse/upward direction.*/

    /*The value returned in step 4 from maiorVector(A,1)=12 is stored in the variable 
    maior in the first statement of else block.*/ 

    int maiorVetor(int A, int 2) {
        int maior;
        if(N == 1) // here 2!=1
            return A[0];
        else {
            maior = maiorVetor(A, N-1); // =maiorVector(A,2-1)=maiorVector(A,1)=12               

           if(maior > A[N-1])
                return maior;
            else
                return A[N-1];
        }
    }

第6步)(参考递归树图)

//as told in Step 3, we had to come back in the maiorVector(A,2) in step 6.
int maiorVetor(int A, int 2) {
        int maior;
        if(N == 1) // here 2!=1
            return A[0];
        else {
            maior = maiorVetor(A, N-1); // =maiorVector(A,2-1)=maiorVector(A,1)=12               

           if(maior > A[N-1]) //if(12>A[2-1])-->if(12>A[1])-->if(12>65), which is 
                              //false
                return maior;
            else
                return A[N-1]; /*Since else part is true, we will return 65.

                               Also, this 65 is returned as the value for maior in the 
                               fucntion maiorVector(A,3) in step 7*/
        }
    }

第7步)(参考递归树图)

//as told in Step 2, we had to come back in the maiorVector(A,3) in step 7.
int maiorVetor(int A, int 3) {
        int maior;
        if(N == 1) // here 3!=1
            return A[0];
        else {
            maior = maiorVetor(A, N-1); // =maiorVector(A,3-1)=maiorVector(A,2)=65               

           if(maior > A[N-1]) //if(65>A[3-1])-->if(65>A[2])-->if(65>14), which is 
                              //true. So return 65.


                return maior; /*Also, this 65 acts as the value for maior for the 
                               fucntion maiorVector(A,4)*/
            else
                return A[N-1]; //false, so not executed.
        }
    }

第8步)(参考递归树图)

//as told in Step 1, we had to come back in the maiorVector(A,4) in step 8.
int maiorVetor(int A, int 4) {
        int maior;
        if(N == 1) // here 4!=1
            return A[0];
        else {
            maior = maiorVetor(A, N-1); // =maiorVector(A,4-1)=maiorVector(A,3)=65               

           if(maior > A[N-1]) //if(65>A[4-1])-->if(65>A[3])-->if(12>14), which is 
                              //true. So return 65.


                return maior; /*Now,this value 65 is returned and printed through the 
                              printf("%d",mainVector(A,4)); statement in the main 
                              function*/
            else
                return A[N-1]; //false, so not executed.
        }
    }

所以,你的输出是

65

【讨论】:

    【解决方案2】:

    正如已经评论过的,理解代码流的更好方法是使用调试器(可能是在线调试器,如下面的参考链接 3 所示)来理解。

    简而言之,maiorVetor(V, N-1)函数被递归调用,直到达到递归的退出条件,即,直到N==1 .在这些步骤中,它将按降序排列整数数组A并返回最大的数,即A[0] em>

    好吧,如果您想了解递归的工作原理以及它如何使用堆栈,请参考以下链接:

    1. https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/
    2. How Recursion works in C
    3. https://www.onlinegdb.com/online_c_compiler1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2013-12-05
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多