Java Basic algorithm problem

Source: Internet
Author: User

In order to improve their ability to code and algorithms, I decided to learn a problem every day to absorb the ideas of predecessors.

"Program 1" Testrabbit.java
Title: Classical Questions: There are a pair of rabbits, from the first 3 months 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?

Program Analysis: The law of the number of rabbits for the series: 1,1,2,3,5,8,13 ... in fact, the Fibonacci sequence can be implemented using recursion.

1 /**2 * Rabbit Problem3 * 2016/5/94 * Fibonacci Sequence Evaluation5 * Title: Classical question: There is a pair of rabbits, starting from the 3rd month after birth a pair of rabbits every month,6 * Rabbits grow to the third month after a month and a pair of rabbits, if the rabbit is not dead, ask the total number of rabbits each month? 7 * Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 ....8  */9  Packageorg;Ten  One ImportJava.util.Scanner; A  Public classTestrabbit { -  -      Public Static voidMain (string[] args) { theScanner input =NewScanner (system.in); -         intn =input.nextint (); -         intnum =Fun (n); -System.out.println ("+n+" month of the total number of rabbits: "+num); +     } -  +      Public Static intFunintN) { A         if(n = = 1 | | n ==2){ at             return1; -}Else{ -             return(n-1) + Fun (n-2)); -         } -          -     } in  -}
Testrabbit.java

Java Basic algorithm problem

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.