✨【Java基础】每天一道基础题+面试题——Day01✨

今天是持续学习的第 29 / 100 天。 如果你有想要交流的想法、技术,欢迎在评论区留言。

🚩一道笔试题

題目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
在这里插入图片描述

public static void main(String[] args) {
        //需要输入的月份:
        System.out.println("请输入的月份");
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int[] arr = new int[i];
        //定义1和2月都只有1对兔子
        arr[0] =arr[1] =1;
        //定义初始月份
        int j =0;
        //try catch 增加稳定性
        try {
            //不确定月份所以弄成while循环
            while (true){
                //前2月
                if (j<2){
                    System.out.println("第"+(j+1)+"个月后有"+arr[j]+"对兔子.");
                    //之后
                } else if (j>=2){
                    arr[j]=arr[j-1]+arr[j-2];

                    System.out.println("第"+(j+1)+"个月后有"+arr[j]+"对兔子.");
                }
                j++;
            }
        } catch (Exception e) {
            System.err.println("到头辣!!");
        }
    }

代码均写有注释了
以上是我的解法
应该还有更优的解法
甚至为了表示999个月,我想到用BigInteger,
具体参考:
https://blog.csdn.net/guomutian911/article/details/45030121

等半年后再回来重做一遍,
看看这梦开始的地方

参考解法:

public class Rubbit {

	public static void main(String[] args) {

	Scanner sc = new Scanner(System.in);
	System.out.print("请输入月份");
	int n = sc.nextInt();
	System.out.println("在"+n+"月份有"+fun(n)+"对兔子");
	}
	private static int fun(int n) {
		if (n == 1 || n == 2)     //  表示第1月,第2月的对数
			return 1;
		else
			return fun(n - 1) + fun(n - 2);  // 3月之后该怎么算
	}

}

🚩一道面试题

2、访问修饰符public,private,protected,以及不写时的区别?

在这里插入图片描述
原文链接:https://blog.csdn.net/v123411739/article/details/115364158
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIMaynor

觉得有用,要个免费的三连可有?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值