兔子繁衍问题(java)

题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
//这是一个菲波拉契数列问题

public class rabbit {
    
    
  public static void main(String[] args) {
    
    
 		System.out.println("第 1 个月的兔子对数:	1");
  		System.out.println("第 2 个月的兔子对数:	1");
  		int f1 = 1, f2 = 1, f, M=24;
  		for(int i=3; i<=M; i++) {
    
     
   		f = f2;
  			f2 = f1 + f2;
  			f1 = f;
  			System.out.println("第" + i +"个月的兔子对数: "+f2);
  		}
  }
}

猜你喜欢

转载自blog.csdn.net/p715306030/article/details/113143175
今日推荐