practice problem bunny

Total Java Rabbits

Topic: Classical problem: There is a pair of rabbits. From the third month after birth, a pair of rabbits is born every month. After the baby rabbit grows to the third month, another pair
of rabbits is born every month. If the rabbits are not dead, ask What is the total number of rabbits per month?

public static void main(String args[]){
    
     
    //i是表示月份的,这里计算了36个月,也就是三年,兔子的数量
	  int i;
	  int arr[]=new int[36];   //这个数组时用来计算每月有兔子的对数
	  arr[0]=arr[1]=1;
	  System.out.println("第1个月有兔子1对"+", "+"总数是"+2);
	  System.out.println("第2个月有兔子1对"+", "+"总数是"+2);
	  for(i=2;i<=35;i++){
    
    
		 arr[i]=arr[i-1]+arr[i-2];
		 System.out.println("第"+i+"个月有兔子"+arr[i]+"对"+", "+"总数是"+2*arr[i]);
		     //规律是 每个数字都是前面两个数字之和
		 }
   }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325074466&siteId=291194637