题解 | #统计每个月兔子的总数#
统计每个月兔子的总数
http://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String numberStr = sc.nextLine();
int number = Integer.parseInt(numberStr);
System.out.println(calNumber(number));
}
}
// 先分析 1--1/2--1/3--2/4--3/5--5/6--8/7--13/8--31/9--34
// 发生后一项是前两项之和
// 采用递归思想
public static int calNumber(int number) {
if(number==1) {
return 1;
}else if(number ==2) {
return 1;
}
if(number>=3 && number <=31) {
return calNumber(number-1)+calNumber(number-2);
}
return 0;
}
}