题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
//假如兔子都不死,问每个月的兔子对数为多少?
//程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
---------------------------------------------------------------------------------------------------------------------
package shiti;
import java.math.BigInteger;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
while (true) {
System.out.println("请输入你想知道第几月兔子的对数:");
String str = new Scanner(System.in).nextLine();
int num = Integer.parseInt(str);
System.out.println("第"+num+"个月的兔子总对数为:"+method(num));
}
}
public static BigInteger method(int num) {
BigInteger[] arr = new BigInteger[num];
if (num == 1||num == 2) {
return new BigInteger("1");
}else {
return method(num-1).add(method(num-2));
}
}
}
|
|