http://www.codewars.com/kata/55bf01e5a717a0d57e0000ec/train/javaCodeWars:持久性的Bugger。需要說明+諮詢
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit. Eg:
39 = [3*9] = 27 = [2*7] = 14 = [1*4] = 4
therefore the output is 3 (the amount of steps)
我試圖這樣KATA,我得到一個錯誤:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
我不知道爲什麼,請誰能幫助?我的代碼如下:我認爲我交付的方式遠非如此複雜,但我嘗試了其他路線,但無法再與他們取得進一步的聯繫。所以這就是我想出的:p.s.即時通訊新的Java和發展,我認爲你可以使用迴歸來解決方案,但我不完全確定。
import java.util.LinkedList;
public class Persist {
static LinkedList<Long> digits = new LinkedList<Long>();
public static void splitNumbersUp(long number){
while(number > 0) {
digits.push(number % 10);
number /= 10;
}
}
public static void clearDigits(){
digits.clear();
}
public static long multiply(int length){
long variable = 0;
for(int i = 0 ; i < digits.size(); i++){
variable = digits.pop() * digits.pop();
digits.push(variable);
}
return variable;
}
public static int persistence(long n) {
long num = 0;
if(n > 10){
while(!((digits.get(0)/10) >= 1)){
splitNumbersUp(n);
num = multiply(digits.size());
clearDigits();
digits.push(num);
}
int i = (int)digits.get(0).intValue();
return i;
}else{
return 0;
}
}
}
嘿嘿,歡迎SO 如果答案工程和您滿意,請接受它的權利答案(這不同於投票;)) – JeD