HJ37

题目描述
有一只兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第三个月后每个月又生一只兔子,假如兔子都不死,问每个月的兔子总数为多少?

本题有多组数据。

输入描述:
输入int型表示month

输出描述:
输出兔子总数int型

解题思路:分成第一个月的兔子 第两个月的兔子 第三个月及以后的兔子

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        while(sc.hasNextInt()){
            int month =sc.nextInt();
            int a=1;
            int b=0,c=0;
            for(int i = 1;i<month;i++){
                c += b;
                b = a;
                a = c;
            }
        
            System.out.println(a+b+c);
        }
    }        
}
posted @ 2021-01-14 03:03  tanjr  阅读(187)  评论(0)    收藏  举报