public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要查询的月份:");
int month = scanner.nextInt();
int [] arr = new int [month+1];
arr[0] = 1;
arr[1] = 1;
System.out.println("第1个月的兔子有1对,总数是2");
System.out.println("第2个月的兔子有1对,总数是2");
for(int i =2;i<month;i++) {
arr[i] = arr[i-1] + arr[i-2];
System.out.println("第" + (i+1) + "个月的兔子有" + arr[i] + "对, 总数是" + arr[i]*2);
}
}
//斐波那契兔子递归
public static int tu(int num) {
if (num==1 || num ==2) {
return 1;
}else {
return tu(num-1)+tu(num-2);
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章