兔子生兔子

/*
    有一只兔子
        出生后第三个月起每个月都生一只兔子
            小兔子长到第三个月后每个月又生一只兔子
    问题:假如兔子死不了,每个月的兔子总数为多少
*/
import java.util.*;

public class Main{
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int num = sc.nextInt();
            int a = 1, b = 1, res = 0;
            for(int i = 0;i < num - 2;i++){
                res = a + b;
                a = b;
                b = res;
            }
            System.out.println(res);
        }
    }
}
发布了120 篇原创文章 · 获赞 37 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Zhengxinyu666/article/details/98125215