2014-10-31 104 views
0

這是學校的作業。我無法理解我怎麼可以打印遞歸如下:以間距遞歸地打印文字

This was written by call number 2. 
This was written by call number 3. 
    This was written by call number 4. 
    This ALSO written by call number 4. 
    This ALSO written by call number 3. 
This ALSO written by call number 2. 
This ALSO written by call number 1. 

我不知道我是否應該是說明循環與遞歸或是否有遞歸打印所有的這一種方式。此外,我將如何去逆轉遞歸調用,以便從示例輸出的4開始?

這是我目前的輸出。

This was written by call number 2. 
This was written by call number 3. 
This was written by call number 4. 
This ALSO written by call number 1. 
    This ALSO written by call number 2. 
    This ALSO written by call number 3. 
    This ALSO written by call number 4. 

沒有在執行for循環B/C我不知道如果這部分也應該是遞歸的間距。

我的代碼:

public class Recursion { 

    public static void main(String[] args) { 
    for (int i = 2; i < 5; i++) { 
     System.out.println("This was written by call number " + i + "."); 
    } 
    recurse(4); 
    } 

    public static void recurse(int n) { 
    String temp = ""; 

    for (int i = 0; i < n; i++) { 
     temp += " "; 
    } 

    if (n < 2) { 
     System.out.println("This ALSO written by call number " + n + "."); 
    } 
    else { 
     recurse(n - 1); 
     System.out.println(temp + "This ALSO written by call number " + n + "."); 
    } 
} 
+0

爲了使它更容易,我建議使用2個獨特的功能。一個在遞歸調用後打印,另一個在前面打印。 – bestsss 2014-10-31 23:38:02

+0

@MichaelJames不,不需要使用2個函數。我正在編寫一個答案...... – ajb 2014-10-31 23:48:17

+0

你可以用一個函數來完成它,然後可能需要硬編碼4,或者將它作爲第二個參數。通常我會寫遞歸計數 - 只需要一個函數來增加計數器。無論哪種方式,遞歸函數都是大約4行代碼。 – bestsss 2014-10-31 23:48:58

回答

1

一個簡單的解決方案。

public static void main(String[] args) { 
    recurse(1); 
} 

public static void recurse (int n) { 
    if (n==5) return; 
    String temp=""; 
    for (int i=0;i<n;i++) temp += " "; 
    if (n!=1) { 
    System.out.println(temp + "This was written by call number " + n + "."); 
    } 
    recurse(n+1); 
    temp=" "; 
    for (int i=0;i<n;i++) temp += " "; 
    System.out.println(temp + "This ALSO was written by call number " + n + "."); 
} 
1

編寫最遞歸程序(特別是你給出的分配的)關鍵是要查找包含了同樣的問題的相似,但較小出現更大的問題。

在你的情況下,「更大的問題」是打印6行開始和結束「電話號碼2」。也就是說,電話號碼2到4的打印行。執行此操作的方法是:打印顯示「電話號碼2」的第一行,解決打印電話號碼3到4的4行4到的問題,並打印最後一行說「電話號碼2」。中間的部分是同一問題的發生率較小。這將是遞歸調用。

由於您的較大問題將以「電話號碼2」開頭,而您的較小問題將以較高的電話號碼開頭,因此我建議您安排一些事宜,以便撥打recurse(n+1)而不是recurse(n-1)。如果你這樣做,你需要第二個參數,以便知道何時停止遞歸 - 例如recurse(n+1, last)

希望這將足以讓您在正確的軌道上思考。

+0

我確實想過需要用「n + 1」來「反轉我的遞歸」,但是我得到了一個堆棧溢出b/c,它沒有第二個參數。謝謝。我會看到我能想出什麼..但是,僅僅爲了說明起見,我可以遞歸地打印第一部分:'這是通過編號n'編寫的,或者我應該使用for循環,就像我目前?編輯:你說的「六條線」,所以我認爲我可以做他們要求的一切for循環。無法理解我將如何使用一個功能..還有, – 2014-10-31 23:53:19

+0

@MichaelJames對於電話號碼2的「大問題」是6行;較小的問題是4行。這意味着您的第2個號碼的功能將打印第一行和最後一行,並在其間遞歸。我想如果你想一想,你會找出最後一個問題的答案。 – ajb 2014-10-31 23:58:22

+0

AH!我沒有這樣想過!謝謝。 – 2014-11-01 00:02:11

1

試試這個:

public static void main(String[] args) { 
    recurse(1, true, 1); 
} 

public static void recurse(int n, boolean loop, int add) { 
    String temp = ""; 
    String out = ""; 

    for (int i = 0; i < n; i++) { 
     temp += " "; 
    } 

    if (add > 0) { 
     out = temp + "This was written by call number "; 
    } else { 
     out = temp + "This ALSO written by call number "; 
    } 

    if (n == 1 && !loop) { 
     System.out.println(out + n + "."); 
     return; 
    } else if (n == 1) { 
     recurse(n+add, false, add); 
    } else if (n == 5) { 
     add = add - 2 * add; 
     recurse(n+add, false, add); 
    } else { 
     System.out.println(out + n + "."); 
     recurse(n+add, false, add); 
    } 
} 
+0

解決方案應該簡單得多 – bestsss 2014-10-31 23:55:21

+0

這裏已經很晚了。 :) – 2014-10-31 23:57:01

+0

從你的名字來看,這將是00:57這不是那麼晚,也就是說我相信我住在你的位置東邊 – bestsss 2014-10-31 23:57:52

1

這是一個非常簡單的解決方案。另外請注意如何輕鬆獲取縮進字符串(通過子字符串)。遞歸過程非常簡單:打印數字,如果低於最大數字,則輸入數字較大的函數,然後返回。

class R{ 
     static final String spaces="         "; 
     public static void main(String[] args) { 
     rec3(1,4); 
     } 
     private static void rec3(int i, int max) { 
     if (i>1) System.out.printf("%sThis was written by call number: %d%n", spaces.substring(0, i-1), i); 
     if (i<max) rec3(i+1, max); 
     System.out.printf("%sThis was ALSO written by call number: %d%n", spaces.substring(0, i-1), i);  
     } 
    } 
0

感謝大家的幫助。我最終修改了@JoseLuis的解決方案。

public class Recursion { 

    public static void main(String[] args) { 
    recurse(1, 5); 
    } 

    public static void recurse(int n, int max) { 
    String temp = ""; 
    for (int i = 0; i < n; i++) { 
     temp += " "; 
    } 
    if (n == max) { 
     return; 
    } 
    if (n != 1) { 
     System.out.println(temp + "This was written by call number " + n + "."); 
    } 
    recurse(n + 1, max); 
    System.out.println(temp + "This ALSO was written by call number " + n + "."); 
    } 
}