public class Test_斐波那契数列 {
public static void main(String[] args) {
fun(93);
}
// 求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
/*
* 分析: 1,n>=1&&n<30; 2,forloop 3,n=1;num=1 2 1 3 第92项=7540113804746346429
* 第93项=-6246583658587674878
*/
public static void fun(int n) {
long[] arr = new long[n];
arr[0] = 1;
arr[1] = 1;
System.out.print("数列前" + n + "项:" + 1 + " " + 1 + " ");
for (int x = 2; x < arr.length; x++) {
arr[x] = arr[x - 1] + arr[x - 2];
System.out.print(arr[x] + " ");
}
System.out.println();
System.out.println("第" + n + "项=" + arr[n - 1]);
}
}
|
|