Java programming-the number of rabbits in a year

Source: Internet
Author: User

Java programming-the number of rabbits in a year

Question: classical question: there is a rabbit. From the first 3rd months after birth, a rabbit is born every month. After the third month, a rabbit is born every month. If the Rabbit does not die, what is the total number of rabbits per month?
1. program analysis: the rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 ....

The typical polasche problem is not mentioned much, and the code is ready directly.

Import java. util. arrayList; public class SecondThread {static ArrayList <Integer> list = new ArrayList (); int number; // calculate the number of rabbits in the next month. public static void main (String [] args) {SecondThread st = new SecondThread (); st. fun (); st. out ();} private void fun () {// Save the number of rabbits for 12 months into the list set. add (1); list. add (1); // number of 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 the rabbit count for 12 months int I = 1; for (int j: list) {System. out. println ("day" + (I ++) + "the number of rabbits in the month is" + j );}}}

 

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.