bailong

题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一

对兔子,假如兔子都不死,问每个月的兔子总数为多少?

1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....

public class Test_1 {

       /**

        * @param args

        */

       public static void main(String[] args) {

              // TODO 自动生成方法存根

              int t1=1,t2=1,t3=1;

              for(int i=1;i<=20;i++){

                     if(i==1||i==2){

                            System.out.println("第"+i+"个月的兔子总数为:"+t3*2);

                     }

                     else{

                            t3=t1+t2;

                            System.out.println("第"+i+"个月的兔子总数为:"+t3*2);                    

                            t1=t2;

                            t2=t3;

                     }

              }

       }

}

分类:

技术点:

相关文章:

  • 2022-02-27
  • 2022-12-23
  • 2021-11-24
  • 2021-12-24
  • 2022-01-28
  • 2021-11-13
  • 2021-10-17
猜你喜欢
  • 2022-02-09
  • 2022-12-23
  • 2021-05-16
相关资源
相似解决方案