题解 | #统计每个月兔子的总数#
统计每个月兔子的总数
https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int months = sc.nextInt(); if(months < 3){ System.out.println(1); } else{ int temp = 0; // 记录本月新生兔 int newBirth = 1; // 记录年满一月兔 int oneMonth = 0; // 记录成年兔 int audltMonth = 0; for(int i = 1; i < months; i++){ // 缓存本月新生兔 temp = oneMonth + audltMonth; // 本月成年兔 audltMonth= audltMonth + oneMonth; // 本月年满一月兔 oneMonth = newBirth; // 本月新生兔 newBirth = temp; } System.out.println(newBirth + oneMonth + audltMonth); } } }