Java Classic Bunny problem

Source: Internet
Author: User

Title: Classical Questions: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the rabbit long to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask each month the total number of rabbits?

1. Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 ....

Package Org.llh.demo01;import Java.util.arraylist;public class DemoTest002 {static arraylist<integer> list = new ArrayList ();    int number;//count of rabbits after one months public    static void Main (string[] args) {    DemoTest002nd = new DemoTest002 ();        St.fun ();        St.out ();    }        private void Fun () {//The number of rabbits for 12 months is saved in the list linked list        list.add (1);        List.add (1); Rabbits in the first two months for        (int i = 3;i<=12;i++) {Number            = List.get (i-2) +list.get (i-3);            List.add (number);        }    }        private void Out () {//Output 12-month rabbit number        int i =1;        for (int j:list) {            System.out.println ("+" + (i++) + "Rabbit number of months is" +j);}}}    

  

12 months Total

Package Org.llh.demo01;public class DemoTest001 {static int month[] = {1,2,3,4,5,6,7,8,9,10,11,12};//months static in      t TYPE = 1;  Couple of pairs of        private int getcount (int month, int type) {          int sum = 0;          if (month = = 1 | | month ==2) {              sum = 1;          } else{              sum = GetCount (month-1,1) + GetCount (month-2,1) *type;          }          return sum;      }            public static void Main (string[] args) {          int count=0;      for (int a:month) {                    int sum = (new DemoTest001 ()). GetCount (A, TYPE);          Count = Sum+count;          }    System.out.println (count);              }  }

  

Java Classic Bunny problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.