• 题目:有一对兔子,从出生第三个月起每个月都生一对兔子,小兔子长到第三个月后,每个月又生一对兔子,假如兔子都不死,问M个月时兔子的数量,M为键盘读入的正整数。(请用java语言作答)


    样例输入:
     
    3
     
    样例输出:
     
    第1个月的兔子对数:1
    第2个月的兔子对数:1
    第3个月的兔子对数:2

    import java.util.Scanner;

    /**
    * @author ForeverLover
    */
    public class Rabbit {
    public static void main(String[] args) {
    long s1 = 1;
    long s2 = 1;
    int count;
    long temp;
    Scanner in = new Scanner(System.in);
    count = in.nextInt();
    for (int i = 1; i <= count; i++) {
    if (i == 1) {
    System.out.PRintln("第" + i + "个月的兔子对数:" + s1);
    continue;
    } else if (i == 2) {
    System.out.println("第" + i + "个月的兔子对数:" + s2);
    continue;
    } else {
    temp = s2;
    s2 = s1 + s2;
    s1 = temp;
    System.out.println("第" + i + "个月的兔子对数:" + s2);
    }
    }
    }
    }
    注:这涉及到的是斐波那契数列,公式:S(n)=S(n-1)+S(n-2)

      所谓斐波那切数列,又称黄金分割数列,是指这样的一个数列0、1、1、2、3、5、8、13、21、34、&hellip;…(

    当然我们这里是从1开始),

  • 相关阅读:
    [Luogu P4779] 单源最短路径(标准版)
    [Luogu P1659] 拉拉队排练
    [Luogu P3435] OKR-Periods of Words
    [Poj #2127] Greatest Common Increasing Subsequence
    [Poj #2019] Cornfields
    [Poj #1949] Chores
    关于我
    划水记录
    [AGC006C] Rabbit Exercise
    [AGC007C] Pushing Balls
  • 原文地址:https://www.cnblogs.com/hangaozu/p/7544463.html
Copyright © 2020-2023  润新知