6 不死·神兔
阿新 • • 發佈:2019-01-10
* * 問題描述:一對兔子第三個月生一對兔子,每個月繼續生,小兔子漲到第三個月又生兔子,以後每個月也繼續生,兔子不死,問每個月的兔子個數 * 兔子的規律:1,1,2,3,5,8.。。 */ import java.util.Scanner; public class One1rubbit { // //首先這個兔子怎麼實現呢? // 寫到下面我寫不下去了 // /* // * A第一步定義常量 // * B第二步累加 // * C第三步事項判斷鍵盤輸入 // */ // Scanner sc =new Scanner(System.in); // int a=sc.nextInt(); // // //判斷輸入是否等於1,1,2, // if(a=1 ||a=2){ // System.out.println("結果為1"); // }; // else() //人家結題思路:用一個遞迴方法 public static void main(String[] args) { //鍵盤錄入 Scanner sc=new Scanner(System.in); int n=sc.nextInt(); System.out.println("第"+n+"個月兔子總數為:"+total(n)); } public static int total(int n) { if(n ==1||n==2){ return 1; } else{ return total(n-1)+total(n-2); } } }