大斐波那契數

+---------------------------------------------------------------------------------------------------------------------------------

題目描述


定義:
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

---------------------------------------------------------------------

運算結果非常龐大,但運算時間卻也是非常小。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章