62,627
社区成员
发帖
与我相关
我的任务
分享
public class RabitGame {
public static final int N = 10;
public static void main(String[] args) {
int[] rabits = new int[N+1];
rabits[0] = rabits[1] = rabits[2] = 2;
for(int i=3; i<=N; i++) {
rabits[i] = rabits[i-1] + rabits[i-3];
}
for(int i=0; i<rabits.length; i++) {
System.out.println(i + "个月后兔子的数量为:" + rabits[i]);
}
}
}
/*
0个月后兔子的数量为:2
1个月后兔子的数量为:2
2个月后兔子的数量为:2
3个月后兔子的数量为:4
4个月后兔子的数量为:6
5个月后兔子的数量为:8
6个月后兔子的数量为:12
7个月后兔子的数量为:18
8个月后兔子的数量为:26
9个月后兔子的数量为:38
10个月后兔子的数量为:56
楼上好快呀,O(∩_∩)O哈哈~
*/
int born = 2;
int first = 0;
int second = 0;
int third = 0;
for (int i = 1; i <= 10; i++) {
third += second;
second = first;
first = born;
born = third;
System.out.println("第" + i + "个月:" + (born + first + second + third));
}