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());
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
斐波数列是一种经典的数学序列,由Leonardo Fibonacci在13世纪提出。而兔子繁殖问题是斐波数列在现实生活中的一个应用。 兔子繁殖问题中,假设一对刚出生的兔子一个月后能够长大并开始繁殖。而每对成年的兔子每个月能够生下一对新的兔子。以此类推,兔子的繁殖数量就符合斐波数列的规律。 以月份为时间单位,第一个月开始只有一对刚出生的兔子。第二个月时,这对兔子长大并开始繁殖,所以兔子的总数量仍为1对。第三个月时,原本的兔子生下了一对新的兔子,总数量变为2对。第四个月时,原本的兔子又生下了一对新的兔子,新的兔子也长大开始繁殖,总数量变为3对。以此类推,每个月兔子的总数量都是前两个月之和,符合斐波数列的特性。 在Java中,我们可以通过编写代码来模拟兔子繁殖问题。首先,我们可以定义一个函数来计算指定月份时兔子的数量,函数接受一个整数参数表示月份。利用递归的思想,我们可以在函数内部调用自身来计算前两个月兔子数量的和,并返回结果。 代码示例: ```java public class Fibonacci { public static int fibonacci(int month) { if (month == 1 || month == 2) { return 1; } return fibonacci(month - 1) + fibonacci(month - 2); } public static void main(String[] args) { int month = 10; int rabbitCount = fibonacci(month); System.out.println("第" + month + "个月时的兔子数量为:" + rabbitCount); } } ``` 上述代码演示了如何计算第10个月时兔子的数量。运行程序会输出结果:第10个月时的兔子数量为:55。 通过这个视频,我们可以更直观地观察到斐波数列兔子繁殖问题中的应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值