题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月
又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
public class Rabbit {
public static void main(String[] args) {
int month;
int f1 = 1;
int f2 = 1;
int f;
System.out.println(“第1个月有”+f1+“只兔子”);
System.out.println(“第2个月有”+f2+“只兔子”);
for(int i = 3 ; i < 24 ; i++)
{
f= f2; //f2记录上个月的兔子数,f1记录上上个月兔子数;
f2 = f1 + f2; //本月的兔子个数;
f1 = f; //为下个月兔子数做准备,将上个月兔子数赋值给f1;
System.out.println(“第”+i+“个月有”+f2+“只兔子”);
}
}
}