簡體   English   中英

C 中的遞歸 - 數組的最大元素

[英]Recursion in C - Largest element of an array

該程序使用遞歸方法返回數組的最大元素:

#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。但實際上該程序有效,它確實給了我一個真正的解決方案。 我很困惑這個遞歸如何工作。

我會畫一個遞歸樹,然后試着解釋它

我正在修改和減少程序中數組的大小,以便更容易解釋 -

#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;

}

根據我的說法,這就是這個程序中發生的事情-> 代碼的遞歸樹


說明-(參考遞歸樹圖)

Step 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*/ 
    }
}

步驟2) (參考遞歸樹圖)

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];
    }
}

Step 3) (參考遞歸樹圖)

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];
    }
}

Step 4) (參考遞歸樹圖)

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];
    }
}

步驟5) (參考遞歸樹圖)

/*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

正如已經評論過的那樣,理解代碼流程的更好方法是使用調試器(可能是在線調試器,作為下面的參考鏈接 3 給出)來理解相同的內容。

簡而言之, maiorVetor(V, N-1)函數被遞歸調用,直到達到遞歸 i,e 的退出條件,直到 N==1。 在這些步驟中,它將按降序對整數數組A進行排序,並返回最大的數字,即A[0]

好吧,如果您想了解遞歸的工作原理以及它如何使用堆棧,請參閱以下鏈接:

  1. https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/
  2. 遞歸如何在 C 中工作
  3. https://www.onlinegdb.com/online_c_compiler 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM