zoukankan      html  css  js  c++  java
  • Java算法之“兔子问题”

     1 package wulj;
     2 
     3 /**
     4  * Java算法之“兔子问题”:
     5  *         有一只兔子,从出生后第3个月起每个月都生只兔子,小兔子长到第三个月后每个月又生一只兔子,假如兔子都不死,问每个月的兔子总数为多少?
     6  * @Description 
     7  * @author wulj
     8  * @date 2018年6月1日
     9  */
    10 public class WljTest {
    11     /**
    12      * 转化为数学问题如下:
    13      * 1.假设第一个月的兔子数为:a,第二个月的兔子数为:b
    14      * 2.假设第一个月的新兔子数为 :new,老兔子数为:old    即:a=new + old;,则 第二个月b= new + 2*old;  
    15      *   到了第三个月的时候,则第二个月的 new存储的已经是老兔子,新兔子实际是old产生的,所以第三个月的秃子数量为:c= 2*(new + old) + old
    16      * 3.化简后发现 a=new + old
    17      *             b= new + 2*old
    18      *             c= 2*new + 3*old
    19      *             即:c= a + b;
    20      * 因此转换为代码实现如函数:rabbitProblem()
    21      */    
    22     public static void rabbitProblem(){
    23         int sum_rubbit;//每月的兔子总数
    24         int pre_pre_rubbit = 1;//前两个月的兔子数
    25         int pre_rubbit = 1;//前一个月的兔子数
    26         for (int i = 3; i <= 12; i++) {
    27             sum_rubbit = pre_pre_rubbit + pre_rubbit;
    28             pre_pre_rubbit = pre_rubbit;
    29             pre_rubbit = sum_rubbit;
    30             System.out.println("第" + i + "个月的兔子总是为:" + sum_rubbit);
    31         }
    32     }
    33     public static int rabbitProblem_digui(int month){
    34         int sum=0;
    35         if(month == 1 || month == 2){
    36             return 1;
    37         }else{
    38             sum = rabbitProblem_digui(month-1)+rabbitProblem_digui(month-2);
    39             return sum;
    40         }
    41         
    42     }
    43     
    44     
    45     /**
    46      * 另外一种思路--转化为数学问题如下:
    47      * 1.假设第一个月的兔子数为:a,第二个月的兔子数为:b
    48      * 2.假设第一个月的新兔子数为 :new,老兔子数为:old    即:a=new + old;,则 第二个月b= new + 2*old;  
    49      *   到了第三个月的时候,则第二个月的 new存储的已经是老兔子,新兔子实际是old产生的,所以第三个月的秃子数量为:c= 2*(new + old) + old
    50      * 通过上面的规律我们发现:
    51      * 当月的new的数量和上个月的old数量是相等的,当月的old的数量是上个月的new与上个月的old的数量之和,当月的总数量是当月的new和当月的old的数量之和
    52      * 因此转换为代码实现如函数:rabbitProblem_new()
    53      */    
    54     public static void rabbitProblem_new(){
    55         int sum_rubbit = 0;//每月的兔子总数
    56         int new_rubbit = 1;//前两个月的兔子数
    57         int old_rubbit = 0;//前一个月的兔子数
    58         int temp_new = 0;
    59         //第一个月:
    60         for (int i = 1; i <= 12; i++) {
    61             sum_rubbit = new_rubbit + old_rubbit;
    62             temp_new = old_rubbit;
    63             old_rubbit = old_rubbit + new_rubbit;
    64             new_rubbit = temp_new;
    65             System.out.println("第" + i + "个月的兔子总是为:" + sum_rubbit);
    66         }
    67     }
    68     
    69     /**
    70      * 递归方法
    71      * @param i
    72      * @param new_rubbit
    73      * @param old_rubbit
    74      */
    75     public static void rabbitProblem_new_digui(int i, int new_rubbit,int old_rubbit){
    76         int sum_rubbit = 0;//每月的兔子总数
    77         int temp_new = 0;
    78         sum_rubbit = new_rubbit + old_rubbit;
    79         if(i == 12){
    80             System.out.println("第" + i + "个月的兔子总是为:" + sum_rubbit);
    81             return;
    82         }else{
    83             temp_new = old_rubbit;
    84             old_rubbit = old_rubbit + new_rubbit;
    85             new_rubbit = temp_new;
    86             rabbitProblem_new_digui(i+1,new_rubbit,old_rubbit);
    87         }
    88         System.out.println("第" + i + "个月的兔子总是为:" + sum_rubbit);
    89         
    90         
    91     }
    92     public static void main(String[] args) {
    93         rabbitProblem_new_digui(1,1,0);
    94     }
    95 }
  • 相关阅读:
    [mysql] information_schema数据库表
    Linux 进程操作_12
    Linux 标准输入输出_11
    apache2
    poj 3083 Children of the Candy Corn 夜
    poj 2151 Check the difficulty of problems 夜
    poj 3274 Gold Balanced Lineup 夜
    poj 3414 Pots 夜
    poj Finicky Grazers 3184 夜
    poj 3253 Fence Repair 夜
  • 原文地址:https://www.cnblogs.com/caogen1991/p/9120550.html
Copyright © 2011-2022 走看看