题解 | #统计每个月兔子的总数#
统计每个月兔子的总数
https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
有点冗杂,但是应该比较好理解。
package OnlineTest.easy; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class HJ37 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int total = Integer.parseInt(br.readLine()); // ArrayList<String> rabbit = new ArrayList(); List<Integer> live = new ArrayList(); //放入第一只兔子和记录它活了多久 rabbit.add("r1"); live.add(1); //开始移动时间 for (int i = 0; i < total; i++) { for (int j=0;j<live.size();j++) { if (live.get(j) >= 3) { //生了新兔子 rabbit.add("rnew"); live.add(1); } } live=live.stream().map(p ->(p+1)).collect(Collectors.toList()); } System.out.println(rabbit.size()); } }