zoukankan      html  css  js  c++  java
  • Java基础50道经典练习题题目(1)——不死兔神

    【程序 1 不死神兔】
    题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月
    又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
    程序分析: 兔子的规律为数列 1,1,2,3,5,8,13,21....
     
    源码:
    package com.homework.test;
    import java.util.*;
    /*
    分析:
    兔子的规律为数列 1,1,2,3,5,8,13,21....,可以看出第3个月兔子对数为前两个月之和,第4个月兔子对数为第二和第三个月兔子对数之和,若第n-2个月兔子对数为f(n-2),第n-1个月兔子对数为f(n-1),
    
    则第n个月兔子对数f(n)=f(n-2)+f(n-1),当n>3时。n=1或2时,f(n)=1。
    
     */
    public class Rabbits {
        public static void main(String[] args ){
            Scanner s = new Scanner(System.in);
            System.out.println("Please input the month:");
            int num = s.nextInt();
            s.close();
            System.out.println("The totally rabbits is :" + fun(num));
    
        }
    
        //递归
        public static int fun(int n){
            if(n==1 || n==2)
                return 1;
            else
                return fun(n-1)+fun(n-2);
    
        }
    
    }
    

      

    今日事,今日毕。
  • 相关阅读:
    CodeForces 514B
    CodeForces 514A
    UVa 818
    HDU 1003
    UVa百题总结
    UVa 11526
    UVa 12412
    UVa 211
    UVa 1587
    UVa 225 – Golygons [DFS+剪枝]
  • 原文地址:https://www.cnblogs.com/lcpp/p/13045086.html
Copyright © 2011-2022 走看看