+---------------------------------------------------------------------------------------------------------------------------------
题目描述
定义:
f(1)=1, f(2)=1, f(n>2)=f(n-1)+f(n-2)
我们把符合以上定义的序列称为斐波那契序列,现在给你一个数字n,请你求出f(n)。
输入格式
输入包含多组测试数据。每组数据为一个正整数n。
输出
输出对应的f(n)。题目保证结果不会超过1000位数字。
样例输入
100
样例输出
354224848179261915075
+--------------------------------------------------------------------------------------------------------------------------------------
对于斐波拉契数列,一看就会想到递归,我们直接看递归式:
然后编写递归程序,当数据比较大时,我们采用Java的大数运算表示:
package org.jian.graph;
import java.math.BigInteger;
import java.util.Scanner;
public class F2 {
private static BigInteger f1 = new BigInteger("1") ;
private static BigInteger f2 = new BigInteger("2") ;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
BigInteger n = s.nextBigInteger();
long start = System.currentTimeMillis() ;
System.out.println(f(n));
long time = System.currentTimeMillis()-start ;
System.out.println(time+"ms");
}
}
public static BigInteger f(BigInteger n) {
if(n.equals(f1)||n.equals(f2)){
return new BigInteger("1") ;
}else {
return f(n.subtract(f1)).add(f(n.subtract(f2))) ;
}
}
}
运行结果:
------------------------------------------------------------------------------------
35
9227465
2900ms
----------------------------------------------
显然仅仅只是很小的一个数,所耗时间就很大了。
当然,这里的大数运算也许会影响一下性能,我们换成普通的运算:
package org.jian.graph;
import java.util.Scanner;
public class F3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
int n = s.nextInt();
long start = System.currentTimeMillis() ;
System.out.println(f(n));
long time = System.currentTimeMillis()-start ;
System.out.println(time+"ms");
}
}
public static int f(int n) {
if(n==1||n==2){
return 1 ;
}else {
return f(n-1)+f(n-2) ;
}
}
}
运算结果:
-----------------------------------------------------------------------------------------
35
9227465
85ms
------------------------------------------------------------------------------
显然比大数运算的快很多,但数字大一点就无能为力了。
而我们换一种非递归的方式:
package org.jian.graph;
import java.math.BigInteger;
import java.util.Scanner;
public class F {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
int n = s.nextInt();
long start = System.currentTimeMillis() ;
System.out.println(f(n));
long time = System.currentTimeMillis()-start ;
System.out.println(time+"ms");
}
}
public static BigInteger f(int n) {
BigInteger f1 = new BigInteger("1");
BigInteger f2 = new BigInteger("1");
BigInteger temp = new BigInteger("0");
for (int i = 2; i < n; i++) {
temp = f2;
f2 = f1.add(f2);
f1 = temp;
}
return f2;
}
}
运行结果:
-------------------------------------------------------------------------------------------
35
9227465
3ms
100
354224848179261915075
1ms
----------------------------------------------
当我们都把n运行35时,所用时间相差上千倍;
我们做一个更大的测试,当N=10000时,第二种方法所需的时间:
---------------------------------------------------------------------------------------
10000
336447648764317832666216120051075433103021484606800639065647699746800814421666623681555955136337340255820653326808361593737347904838652682630408924630564318873545443695598274916066020998841839338646527
313000888302692356736131351175792974378544137521305205043477016022647583189065278908551543661595829872796829875106312005754287834532155151038708182989697916131278562650331954871402142875326981879620469
360978799003509623022910263681314931952756302278376284415403605844025721143349611800230912082870460889239623288354615057765832712525460935911282039252853934346209042452489294039017062338889910858410651
831733604374707379085526317643257339937128719375877468974799263058370657428301616374089691784263786242128352581128205163702980893320999057079200643674262023897831114700540749984592503606335609338838319
233867830561364353518921332797329081337326426526339897639227234078829281779535805709936910491754708089318410561463223382174656373212482263830921032977016480547262438423748624114530938122065649140327510
866433945175121615265453613331113140424368548051067658434935238369596534280717687753283482343455573667197313927462736291082106792807847180353291311767789246590899386354593278945237776744061922403376386
740040213303432974969020283281459334188268176838930720036347956231171031012919531697946076327375892535307725523759437884345040677155557790564504430166401194625809722167297586150269684431469520346149322
911059706762432685159928347098912847067408620085871350162603120719031720860940812983215810772820763531866246112782455372085323653057759564300725177443150515396009051686032203491632226408852488524331580
515348496224348482993809050704834824493274537326245677558790891871908036620580095947431500524025327097469953187707243768259074199396322659841474981936092852239450397071654431564213281576889080587831834
0491743455627052022356484649519611246026831397097506938264870661326450766507461151267752274862159864253071129844118262266105716351506926002986170494542504749137811515413994155067125627119713325276363193
9606902895650288268608362241082050562430701794976171121233066073310059947366875
56ms
---------------------------------------------------------------------
运算结果非常庞大,但运算时间却也是非常小。