我試圖產生一系列前術語 https://oeis.org/A004207Java程序遞歸不工作
1,1,2,4,8,16,23,28,38,49,62的數字的總和的,70,77,91,101,103,107,115,122,127,
這是我有
import java.math.BigInteger;
import java.util.ArrayList;
public class Problem {
public static int getDigitSum(int number) {
int total = 0;
while (number>0) {
total += total + number%10;
number = number /10;
}
return total;
}
public static int getSum(int number) {
if ((number == 0) || (number == 1)) // base cases
return number;
else
// recursion step
return getSum(getDigitSum(number - 1)) + getSum(number - 2);
}
public static void main(String s []) {
int sum =0, difference=1, previous =1, last;
//ArrayList <Integer> sum_list = new ArrayList <Integer>();
//sum_list.add(prevous);
for (int i = 1; i<10; i++) {
System.out.println(getSum(i));
}
}
}
該代碼的代碼產生錯誤的輸出
1
1
2
3
5
8
13
21
34
那些人下來投票,請評論的反對投票 – karu
我投票關閉這一問題作爲題外話,因爲OP沒有按」的原因期待任何人的回答。 –
@AndyTurner 1 + 1 + 2 + 4 + 8 + 16,其中16 = {1 + 6} = 7 – karu