(Java - TechFund) Arrays, more exercises, задача Recursive Fibonacci
Здравейте, някой дали може да ми каже защо в Judge това решение има 85/100 (един тест дава Runtime Error):
import java.util.Scanner;
public class RecursiveFibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
long[] fibonacci = new long[n];
for (int i = 0; i < n; i++) {
if (i == 0 || i == 1) {
fibonacci[i] = 1;
} else {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
}
System.out.println(fibonacci[n - 1]);
}
}
Докато това решение има 100/100:
import java.util.Scanner;
public class RecursiveFibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
long first = 0;
long second = 1;
long third = second + first;
for (int i = 0; i < n; i++) {
first = second;
second = third;
third = first + second;
}
System.out.println(first);
}
}
Благодаря предварително!