2013-04-23 132 views
1

我有一個因子程序寫下來。我已經完成了整個程序,運行沒有任何問題。 不過,我想輸出看起來像這樣因子程序打印出遞歸

Begin Factorial Program. . . 
Provide number and the factorial will be computed: 4 
4! is . . . 
In f(4) and calling f(3) . . . 
In f(3) and calling f(2) . . . 
In f(2) and calling f(1) . . . 
In f(1) and calling f(0) . . . 
    In f(0) and computing f(0). Returning 1 
f(1) is 1 * 1 which equals 1. Returning 
f(2) is 2 * 1 which equals 2. Returning 2 
f(3) is 3 * 2 which equals 6. Returning 6 
f(4) is 4 * 6 which equals 24. Returning 24 
4! = 24 

我怎麼得到的。

f(1) is 1 * 1 which equals 1. Returning 
f(2) is 2 * 1 which equals 2. Returning 2 
f(3) is 3 * 2 which equals 6. Returning 6 
f(4) is 4 * 6 which equals 24. Returning 24 

打印出我的方法

這是方法我有

public static int factorial(int num) { 
    if (num == 0) { 
     System.out.println("In f(" + num + ") and computing f(" + num 
       + "). Returning " + 1); 
     return 1; 
    } else { 
     System.out.println("In f(" + num + ") and calling f(" + (num - 1) 
       + ") . . ."); 


     return num * factorial(num - 1); 
    } 

打印出

4! is . . . 
In f(4) and calling f(3) . . . 
In f(3) and calling f(2) . . . 
In f(2) and calling f(1) . . . 
In f(1) and calling f(0) . . . 
In f(0) and computing f(0). Returning 1 
4! = 24 

回答

1

像這樣的東西應該做的伎倆:

public static int factorial(int num) { 
    if (num == 0) { 
     System.out.println("In f(0) and computing f(0). Returning 1"); 
     return 1; 
    } 

    System.out.printf("In f(%d) and calling f(%d) . . .%n", num, 
       num - 1); // #1 

    int factorial = factorial(num - 1); 
    int result = num * factorial; 

    System.out.printf(
     "f(%1$d) is %1$d * %2$d which equals %3$d. Returning %3$d%n", 
      num, factorial, result); // #2 

    return result; 
} 
 
In f(4) and calling f(3) . . . 
In f(3) and calling f(2) . . . 
In f(2) and calling f(1) . . . 
In f(1) and calling f(0) . . . 
In f(0) and computing f(0). Returning 1 
f(1) is 1 * 1 which equals 1. Returning 1 
f(2) is 2 * 1 which equals 2. Returning 2 
f(3) is 3 * 2 which equals 6. Returning 6 
f(4) is 4 * 6 which equals 24. Returning 24 
24 

注意如何factorial遞歸調用兩個打印語句之間夾(我們這樣做是通過將其存儲在一個新的變量反對在線使用其結果)。因此,所有第一個打印語句(#1)都會在第二個打印語句(#2)之前執行,從而生成所需的格式。

此外,在這種情況下,printf更有意義,使事情變得更容易閱讀/開發。

0

你回來直出,不給自己製作您要求的額外日誌語句。你需要做的是設置一個等於返回的變量,然後你可以打印出你的「返回」語句,然後你可以返回。

+0

當然,然後你失去了你的漂亮的尾遞歸設置。但對於一個可能不是世界末日的玩具例子來說。 – dfreeman 2013-04-23 21:49:04

+0

@dfreeman'return num * factorial(num-1);'不是尾遞歸的。不過,一個好的編譯器可以將它轉換爲尾遞歸(或迭代),但它並不重要。 – 2013-04-23 22:43:07

+0

@danielfischer哎呀,你是對的。我沒有想到 - 我的壞評論。 – dfreeman 2013-04-24 15:15:20

0

取出「factorial(num-1);」從return語句調用並將其保存在一個臨時變量中。

e.x.

int retVal = factorial(num-1) 
System.out.println("I'm about to return "+retVal); 
return retVal; 
0
public static int factorial(int num) 
{ 
    int ret =0; 
    if (num == 0) 
    { 
     System.out.println("In f(" + num + ") and computing f(" + num+ "). Returning " + 1); 
     ret = 1; 
    } 
    else 
    { 
     System.out.println("In f(" + num + ") and calling f(" + (num - 1)+ ") . . ."); 
     int fact = factorial(num - 1); 
     System.out.println("f("+num+") is "+num + " * "+fact + " which equals "+ num*fact+". Returning "+num*fact); 
     ret = num*fact; 
    } 
    return ret; 
}