【for 循环计算】 有一对兔子,三天后生一对兔子。每对兔子都如此。

来源:互联网 发布:windows 访问samba 编辑:程序博客网 时间:2024/04/29 13:57

package www.wtt.Test;
/**
 * 有一对兔子,三天后生一对兔子。每对兔子都如此。
 * 假设兔子不死,输出第几个月有多少对兔子
 * @author 123
 *
 */
public class tuzi {
 public static void main(String[] args) {
  cal(8);//第8个月
 }
 public static void cal(int month){
  int t=0;//对
  int t1 = 0,t2 = 0;
  for(int i=1;i<month;i++){ 
   if(i<3){
    t1=1;
    t2=1;
   System.out.println("第"+i+"个月,兔子一共"+1+"对");
   }
   else{
    t=t1+t2;
    t1=t2;
    t2=t;
    System.out.println("第"+i+"个月,兔子一共"+t+"对");
    }  
  }
 }
}

 

0 0
原创粉丝点击