编程4 兔子生崽问题--一个Fibonacci数列

/*
  兔子生崽问题--Fibonacci数列
  有一对兔子。从出生后第三个月起每个月生一对兔子,小兔子长到第三个月后每个
  月有生一对兔子,假如兔子都不死,问每个月的兔子总数是多少。
  
 */
package com.test1;

public class lianxi01 {

	public static void main(String[] args) {

		final int YEAR=12;
		int[] f=new int[13];//1月和2月兔子对数分别初始化为1,2,不使用f[0]
		f[0]=0;
		f[1]=1;
		f[2]=2;
		int month;
		
		//从3月份开始计算每月总的兔子对数
		for(month=3;month<=YEAR;month++)
		{
			f[month]=f[month-1]+f[month-2];
		}
		//输出12个月的总兔子对数
		for(month=1;month<=YEAR;month++)
		{
			System.out.println(month+" 月兔子有 "+f[month]+" 只");
		}
		
	}

}

你可能感兴趣的:(编程实战)