题解 | #统计每个月兔子的总数#
统计每个月兔子的总数
https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = 1;//兔子总数
int[] status = new int[3];
/*兔子的状态:int[3]分别对应
*int[0]第一个月:不能生
*int[1]第二个月:不能生
*int[2]第三个月:能每个月生*/
status[0] = 1;
int born=0;
for(int i = 2;i<=n;i++){
status[2] += status[1];
status[1] = status[0];
born = status[2];//本月所生
status[0] = born;
m += born;
born=0;
}
System.out.print(m);
}
}