package gxx20;
import java.util.Scanner;
//斐波那契数列:第n个月有m只兔子
//1,1,2,3,5,8,13,21...
public class FeiBoNaQieDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入月份:");
int n = scanner.nextInt();
System.out.println("第" + n + "个月有兔子:" + m(n) + "只");
}
//递归
private static int m(int n) {
if (n == 1 | n == 2) {
return 1;
} else {
return m(n - 1) + m(n - 2);
}
}
import java.util.Scanner;
//斐波那契数列:第n个月有m只兔子
//1,1,2,3,5,8,13,21...
public class FeiBoNaQieDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入月份:");
int n = scanner.nextInt();
System.out.println("第" + n + "个月有兔子:" + m(n) + "只");
}
//递归
private static int m(int n) {
if (n == 1 | n == 2) {
return 1;
} else {
return m(n - 1) + m(n - 2);
}
}
}
运行结果: