7-12 兔子跳楼梯 高精度 java 斐波那契数列

小兔子喜欢蹦蹦跳跳上楼梯 ,它能一次跳1阶楼梯,也能一次跳上2阶楼梯。问小兔子要上一个n阶的楼梯,最多有多少种不同上楼的走法?
输入格式:

输入一行包含一个整数 n,表示有几阶楼梯。
输出格式:

上楼梯的走法数
输入样例:

3

输出样例:

3

评测用例规模与约定

对于 20%的评测用例,1≤n≤10。 对于 50%的评测用例,1≤n≤100。 对于 80%的评测用例,1≤n≤1000。 对于所有评测用例,1≤n≤10000。

注意点 :
裸的斐波那契数列f[n]=f[n-1]+f[n-2]
java提交记得把package xxx删掉,不然会wa


//package leetcode;

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

	private static final boolean debug = false; //条件编译
	public static long startTIme, endTime;
	public final static int MAXN = (int) 1e5 + 7;
	public final static int INF = 0x7f7f7f7f;

	public static int n, m, Q, K;

	public static void main(String[] args) throws Exception {
		if (debug) {
			FileInputStream fis = new FileInputStream("/home/majiao/桌面/test");
//			System.setOut(new PrintStream("/home/majiao/桌面/out"));
			System.setIn(fis);
			startTIme = System.currentTimeMillis();
		}

		Read cin = new Read(System.in);
		n = cin.nextInt();
		if(n == 1) {
			printf("%d\n", 1);
			return ;
		} else if(n == 2) {
			printf("%d\n", 2);
			return ;
		}
		BigInteger now = new BigInteger("0"),
				lst = new BigInteger("2"), llst = new BigInteger("1");
		for(int i=3; i<=n; i++) {
			now = lst.add(llst);
			llst = lst;
			lst = now;
		}
		System.out.println(lst);
		
		if (debug) {
			showRunTime();
		}
	}

	static final void printf(String str, Object... obj) {
		System.out.printf(str, obj);
	}

	static final void show(Object... obj) {
		for (int i = 0; i < obj.length; i++)
			System.out.println(obj[i]);
	}

	static final void showRunTime() {
		endTime = System.currentTimeMillis();
		System.out.println(
				"start time:" + startTIme + "; end time:" + endTime + "; Run Time:" + (endTime - startTIme) + "(ms)");
	}

}

class Read { //自定义快读 Read

	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public Read(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public String nextLine() {
		String str = null;
		try {
			str = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public Double nextDouble() {
		return Double.parseDouble(next());
	}

	public BigInteger nextBigInteger() {
		return new BigInteger(next());
	}
}


你可能感兴趣的:(java,斐波那契数列,BigInteger)