import java.util.*; public class Main{ public static long[] arr = new long[91]; public static void fun(){ arr[0] = 1; arr[1] = 1; arr[2] = 0; for(int i = 2;i < arr.length;i++){ arr[i] = arr[i - 1] +arr[ i - 2]; } } // public static void main(String[] args){ // Scanner sc = new Scanner(System.in); // while(sc.hasNext()){ // int n = sc.nextInt(); // fun(); // System.out.println(arr[n]); // } // } //方法二: public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); long f0 = 1; long f1 = 1; long f2 = 1; for(int i = 2;i <= n;i++){ f2 = f1 +f0; f0 = f1; f1 = f2; } System.out.println(f2); } } }
//由题意可知数列为:1 2 3 5 8 13 .。。 //即知该数列为斐波那契数列(从第1项开始的斐波那契) //解决思路:利用特性:f(n)=f(n-1)+f(n-2); //即可用迭代法的方法算出第n个斐波那契值,这里为了防止溢出,类型用long //若long还是溢出,此时应换种思路去解(备注:用java的BigInteger虽然可以得出答案,可消耗了一定的时间复杂度,不建议选) //思路2:用数组存储斐波那契数列值,用二维数组存储每一行代表第N个数,每一列为f斐波那契值的前几位(采用取模即可) import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); // int n = sc.nextInt(); while(sc.hasNext()){ int t = sc.nextInt(); long sum = 1; if(t == 1){ System.out.println(sum); }else{ long a = 1; long b = 1; for(int i=2;i<=t;i++){ sum = a + b; a = b; b = sum; } System.out.println(sum); } } } }
//就是斐波那契数列 import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); long[] arr = new long[91]; arr[1] = 1; arr[2] = 2; for(int i = 3;i < arr.length;i++){ arr[i] = arr[i-1] + arr[i-2]; } System.out.println(arr[n]); } } }