public class c06_03_01兔子产子问题 { /** * 递归,算出几个月后兔子的数量 * @param month 月份 * @return 兔子量 */ public static int rabbitNum(int month){ if(month <= 0){ return 0; } else if(month <= 2){ return 1; }else { return rabbitNum(month-1)+ rabbitNum(month-2); } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (true) { System.out.printf("请输入你要求第几个月的兔子量:"); int month = scan.nextInt(); System.out.printf("第%d个月有%d对兔子\n", month, rabbitNum(month)); } } }
兔子产子问题
最新推荐文章于 2023-01-01 14:49:39 发布