簡體   English   中英

有人可以幫助我理解這兩段代碼[遞歸]嗎?

[英]Can someone help me understand these 2 pieces of code [Recursion]?

我很難理解這段代碼中發生的事情。 在針對作業的14個問題中,這是我唯一遇到的兩個問題,只是在不知道答案如何的情況下給出答案。 任何幫助,將不勝感激,謝謝。

這是#1:

public void printDollarSign(int k)
{
        int j;
        if (k>0)
        {
              for (j=1; j<= k; j++)
                  System.out.print("$");
              System.out.println();
              printDollarSign(k-1);
        }
}

如果調用是,輸出將是什么:printDollarSign(5);

答案是:

$$$$$

$$$$

$$$

$$

$

這是#2:

public void bbb(String s, int p)
{
    if (p>= 0)
    { 
        bbb(s,p-1);
        System.out.print(s.charAt(p)); 
    }
}

如果調用是,輸出將是什么:bbb(“ January”,4);

答案是:

亞努阿

printDollarSign的說明:

  • 在這里,您創建了一個遞歸函數。
  • 每個遞歸函數都有一個基本條件,在您的情況下是if(k> 0)。
  • 每個遞歸調用都會將k的值遞減1.因此,對於每個遞歸調用,for循環運行的次數將減少1。

對於首次通話:

  1. 假設k = 5;
  2. 函數調用printDollarSign(5);
  3. 檢查是否(k> 0)即5> 0;
  4. For循環運行5次並打印5個“ $”符號;
  5. println打印換行符;
  6. 遞歸調用printDollarSign(4);

printDollarSign(k)首先打印k個$,然后打印換行,然后調用printDollarSign(k-1)首先打印k-1個$,然后打印換行,然后調用printDollarSign(k -1-1)...一直持續到k = 0。 當k = 0時,printDollarSign(0)不打印任何內容。

您的第一個方法是創建一個帶有int特定參數的函數,在本例中為“ k”。

  • 然后通過“ int k;”設置一個稱為j的int。
  • 然后檢查k是否大於0
  • 檢查后,它遍歷k(其中j = 1且j <k)並打印出“ $”符號
  • 它將產生“ $$$$$” ..“ $$$$” ..等,直到k <0
  • 然后編寫新行,然后再次調用該函數,將k減1

碼:

public void printDollarSign(int k){ //Define function
        int j;//Define j as an int
        if (k>0){//Check if k is greater than 0
             for (j=1; j<= k; j++)//Loop through k where j = 1
                 System.out.print("$");//Print $ by the amount of k
             System.out.println();//Print a new line
             printDollarSign(k-1);//Re run the function
        }
}

您的第二個問題是創建一個帶有字符串和int“ s”和“ p”兩個參數的函數

  • 它檢查p是否大於0
  • 然后在內部調用該函數,並減去p(p-1)中的1
  • 然后根據p從字符串中打印出一個字符

碼:

public void bbb(String s, int p){//Define Function
    if (p>= 0){ //Check if p is greater than 0
        bbb(s,p-1);//Rerun function
        System.out.print(s.charAt(p));//Print character of string based on p
    }
}

添加代碼注釋以進行解釋:

1號

public void printDollarSign(int k)
    {
        // temporary variable j, it will be always initialized to 1 inside the for loop.
        int j;
        // only executed to be true if k is more than 0, that means if K is initially 5
        // then it only works for 5,4,3,2,1  and not for 0.
        if (k>0)
        {
            // always starts with 1 and goes to the value of k, that means if K is currently 5
            // then it will print 5 dollars, if 4 then 4 dollars and so on
            for (j=1; j<= k; j++)
                System.out.print("$");
            // a new line will be added once dollars are printed.
            System.out.println();
            // this will again call the same function and decrements the value of k, so next time 
            // k will have the value one less then the previous one, if this has printed 5 dollars 
            // in last iteration next time it will print 4 dollars and so on
            printDollarSign(k-1);
        }
    }

2號

public void bbb(String s, int p)
    {
        // only print a character if p has a value grater than 0. in you case , p has a value 4 that
        // mean only 4 characters will be printed at max
        if (p>= 0)
        { 
            // recuresively call same method by decrementing p, so it will be
            // [bbb(s,3) prints 'a']-> [bbb(s,3) prints 'u']-> bbb(s,2) [prints 'n']-> [bbb(s, 1) prints 'a']> [bbb(s, 0) prints 'J']
            // last character will be printed first
            bbb(s,p-1);
            // prints the character at p location
            System.out.print(s.charAt(p)); 
        }
    }

bbb的解釋:

  • 遞歸的一種應用是從給定的字符串中提取子字符串。
  • 基本條件是否(p> = 0)。
  • 您應該意識到以下事實:對於每個遞歸調用,“ P”的值都存儲在堆棧中。
  • 如您所知,堆棧是一個后進先出(LIFO)數據結構,插入堆棧中的最后一個值將首先彈出。

執行:

  1. bbb(“ January”,4); ->堆棧包含: [4]
  2. bbb(“ January”,3); ->堆棧包含: [3 4]
  3. bbb(“ January”,2); ->堆棧包含: [2 3 4]
  4. bbb(“ January”,1); ->堆棧包含: [1 2 3 4]
  5. bbb(“ January”,0); ->堆棧包含: [0 1 2 3 4]

現在彈出每個元素並在該位置打印字符

  1. 在0之后-> J
  2. 1之后-> Ja
  3. 2點后-> 1月
  4. 3之后-> Janu
  5. 4點后-> Janua
if you provide a string s as 'January', the way it stores is like this:
0th position = J, 
1th position = a,
2th position = n,
3th position = u,
4th position = a,
5th position = r,
6th position = y,
Just like an array of characters. 
When you provide p=4, you are setting the closing criteria for the condition    check. 

function bbb('January', 4){
if(4>=0){
bbb('January', 3); ....

function bbb('January', 3){
if(3>=0){
bbb('January', 2); ....

function bbb('January', 2){
if(2>=0){
bbb('January', 1); ....

function bbb('January', 1){
if(1>=0){
bbb('January', 0); ....

function bbb('January', 0){
if(0>=0){
bbb('January', -1); ....

function bbb('January', -1){
if(-1>=0){ Here condition check fails.. hence char at(-1) doec not print

return goes back to print of previous call and prints J as p=0, we have J
the p=1: a
p=2: n
p=3: u
p=4: a

功能完成...

請讓我知道說明是否有幫助

暫無
暫無

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

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