Recursive - Fibonacci & Bunny

Fibonacci number

Arrangement Fibonacci number is: 0,1,1,2,3,5,8,13,21,34,55,89,144 ...... and so on down.

After observed, after which a number equal to the number of preceding and two. In this series of numbers, it is known as Fibonacci.

Recursive thinking: a number equal to the number of former and two.

package com.autumn;

/**
 * Fibonacci arrangement of Number of
 * 0,1,1,2,3,5,8,13,21,34,55,89,144
 * A number equal to the front and rear two numbers
 */
public class Recursive {

    public static void main(String[] args) {
        for (int i= 0;i<30;i++){
            System.out.println(fib(i));
        }
    }

    /**
     * Fibonacci vertical columns returned value
     * @Param index index, starting from 0
     * @Return index value corresponding to
      * / 
    public  static  int FIB ( int index) {
         / * If the index is 0, returns 0 * / 
        IF (index == 0 ) {
             return 0 ;
        }
        / * If the index is 1, return 1 * / 
        IF (index == 1 ) {
             return 1 ;
        }
        // Otherwise, return to the first two and 
        return FIB (-index. 1) + FIB (index-2 );
    }
}

Rabbit problem

Rabbit question: Initially, 1 rabbit, after three months, the monthly production of a rabbit (the premise, the rabbit will not die), then the number of rabbits after the first month, a total of n

package com.autumn;

/**
 * Raw rabbit
 * A pair of rabbits, after three months of growth. The total number of students began the first pair of rabbits, and a month after birth to a rabbit, a small rabbit growth after three months, students have begun rabbit, ask N months after the rabbit
 * The number of rabbits corresponding month: 1,1,2,3,5,8
 * It can be seen the first three months to raw rabbit pair that is the third month of the total number of existing bunny rabbit = second month of the first month + the number of rabbits can be born (born soon be able to count the number of students =)
 * Recursive manner:
 * (1) rule: the total number of rabbits per month of the total number of rabbits and the first two months
 * (2) Export: the first two months is a pair
 */
public class Recursive {

    public static void main(String[] args) {
        for (int i= 1;i<30;i++){
            System.out.println(rabbit(i));
        }
    }

    /**
     * The total number of rabbits month
     * @Param index in January, starting from 1
     * @Return rabbit value
      * / 
    public  static  int Rabbit ( int index) {
         / * If the month is 1, return 1 * / 
        IF (index == 1 ) {
             return 1 ;
        }
        / * If the month is 2, return. 1 * / 
        IF (index == 2 ) {
             return . 1 ;
        }
        // Otherwise, return to the first two and 
        return Rabbit (-index. 1) + Rabbit (index-2 );
    }
}

 

Guess you like

Origin www.cnblogs.com/aeolian/p/12132307.html