我有兩個以下版本的實現,他們爲什麼返回不同的結果?返回不同的結果來找到Factorial Trailing Zero
陳述問題,
給定一個整數n,返回在正尾隨零的數目!在Java中
源代碼,
public class TrailingZero {
public static int trailingZeroes(int n) {
int result = 0;
int base = 5;
while (n/base > 0) {
result += n/base;
base *= 5;
}
return result;
}
public static int trailingZeroesV2(int n) {
return n == 0 ? 0 : n/5 + trailingZeroes(n/5);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(trailingZeroes(1808548329));
System.out.println(trailingZeroesV2(1808548329));
}
}
歡迎回到Stack Overflow!尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須在問題本身中包含所需的行爲,特定的問題或錯誤以及必要的最短代碼**。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –
我認爲這個問題想要你返回n階乘的尾隨零的數量。 –
在第二次遞歸調用應該是'trailingZeroesV2';一個錯字,是不是。 –