import java.util.Scanner; public class DiTui { public static int fibonacci(int n){ int t1,t2; if(n==1||n==2) { return 1; } else { t1 = fibonacci(n-1); t2 = fibonacci(n-2); return t1 + t2; } } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("地推算法求解兔子产仔问题!"); System.out.print("请先输入时间:"); Scanner input = new Scanner(System.in); int n = input.nextInt(); int num = fibonacci(n); System.out.println("经过"+n+"个月的时间,总共能繁殖成"+num+"对兔子"); } }
时间: 2024-11-05 11:18:47