题解 | #统计每个月兔子的总数#

统计每个月兔子的总数

http://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int month = sc.nextInt();
            int sum = total(month);
            System.out.println(sum);
        }
    }

    public static int total(int month) {
        if (month <= 2) {
            return 1;
        } else {
            return total(month - 2) + total(month - 1);
        }
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务