参考代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = fang(n);
System.out.println(m);
sc.close();
}
public static int fang(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
}
int[] fib = new int[n + 1];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
}
0.0分
1 人评分