public class Test_1_2 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String str1 =sc.nextLine();
String[] all=str1.split(" ");//用字符串数组来接受输入的多个数据,分隔符使用空格
String n1=all[0];
String n2=all[1];
try {
int a = Integer.parseInt(n1);
int b = Integer.parseInt(n2);
int result = fabonacciSum(a,b);
System.out.println(result);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
private static int fabonacciSum(int i, int j) {
if (i > j || i < 0 || j < 0) {
return -1;
}
if (j < 2) {
return j;
}
int[] array = new int[j+1];
int total;
if (i < 2) { //下面的for循环是从下标2开始的
total = 1;
} else {
total = 0;
}
array[0] = 0;
array[1] = 1;
for (int k = 2; k <= j; k++) {
array[k] = array[k-1] + array[k-2];
if (k >= i) {
total += array[k];
}
}
return total; }
}
原文:https://www.cnblogs.com/fishshadow/p/10836425.html