import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long output = fib(n);
System.out.println(output);
}
public static long fib(int n){
if(n == 1){
return 0;
}else if(n == 2){
return 1;
}else{
long a = 0;
long b = 1;
long c = 0;
for(int i = 1; i <= n - 2; i++){
c = a + b;
a = b;
b = c;
}
return c;
}
}
}
这题要用long类型的数据存储,在写一个方法来求数据,再输出。
京公网安备 11010502036488号