2-17统计每个月兔子的总数

题目描述

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

    /**
     * 统计出兔子总数。
     * 
     * @param monthCount 第几个月
     * @return 兔子总数
     */
    public static int getTotalCount(int monthCount)
    {
        return 0;
    }

输入描述:

输入int型表示month

输出描述:

输出兔子总数int型

输入例子:
9
输出例子:
34

代码:
import java.util.*;
public class Main
{
 public  static int getTotalCount(int monthCount){
    if(monthCount==1||monthCount==2)
     return 1;
   
    else
     return getTotalCount(monthCount-1)+getTotalCount(monthCount-2);
 }
 public static void main(String[] args){
 Scanner sc =new Scanner(System.in);
 while (sc.hasNextInt()){
 int n=sc.nextInt();
 int count=getTotalCount(n);
 System.out.println(count);
 }
 }
}

收获:(1)用递归
(2)一开始通不过犯了前面的错,题目没说清楚要输入多组数据,所以要加while (sc.hasNextInt())
(3)注意
print(count);也不对。
posted @ 2017-03-17 00:56  code666  阅读(191)  评论(0编辑  收藏  举报