JAVA算法t1

import java.util.Scanner;

/**
 * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
 * 分析 一月 1对, 2月 1对,3月 2对,4月 3对,5月5对,6月8对,7月13对,8月 21只
 */
public class Subject1 {

    public static void main(String[] args) {
        System.out.print("请输入你想知道的兔子数量的月份:");

        Scanner scanner=new Scanner(System.in);

        int n=scanner.nextInt();//获取输入的整数

        System.out.println(test(n));

        System.out.println(fun(n));

        scanner.close(); 
    }

    /**
     * 菜鸟写法
     * @param i
     * @return
     */
    public static int test(int i){
        int tot = 0;
        int a = 0;
        int b = 1;
        for(int j = 0;j<i; j++){
            tot = a+b;
            a = b;
            b = tot;
        }
        return a;
    }

    /**
     * 大神写法
     * @param
     * @return
     */
    private static int fun(int n){
        if(n==1 ||n==2)
            return 1;
        else
            return fun(n-1)+fun(n-2);
    }
}

scanner.close(); 是什么意思

第18行为什么报错

test(n) 和 fun(n)的意思

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值