What is recursion? Analysis of recursive programming art and Analysis of recursive programming Art

Source: Internet
Author: User

What is recursion? Analysis of recursive programming art and Analysis of recursive programming Art

What is recursion? Recursive programming art parsing: Recursive Algorithms call their own algorithms directly or indirectly. recursion includes the process of handing (calling the next) and returning (the next return.

Recursion is an idea. recursion is not an algorithm. It is a simple way of thinking for complicated problems. This way of thinking is embodied in a program! The implementation of recursive algorithms is the process of constantly calling functions!

What needs to be determined is: 1. Recursive boundary condition 2. Recursive generic

Recursion can first identify the law through a limited number of times and sum up the formula, there is a certain relationship between the value of nth in this formula and the value of nth in the N-1. recursion can be used (the boundary condition must be considered)

VcC0veLKzbvy1d/examples + o6y2 + Mq508O1/examples/samples + samples/samples + bHsKOsyOe5 + samples + u1/LT6tcTQp7n7z + samples = "test tool">Test Tool

package ch06;public class Recursion {     public static void main(String[] args) {           test2(100);     }     public static void test() {           System.out.println("Hello,World!");           test();     }     public static void test2(int n) {           if(n == 0) {                return;           }           System.out.println(n);           test2(n - 1);     }}

 

Triangle number

Package ch06; public class Triangle {// common algorithm public static int getNumber (int n) {int total = 0; while (n> 0) {total = total + n; n --;} return total;} // recursive algorithm public static int getNumberByRecursion (int n) {if (n = 1) {return 1 ;} else {return n + getNumberByRecursion (n-1) ;}} public class TestTriangle {public static void main (String [] args) {System. out. println (Triangle. getNumber (500); System. out. println (Triangle. getNumberByRecursion (500 ));}}

 

Fibonacci Series

Package ch06; public class Fibonacci {public static int getNumber (int n) {// the first two items if (n = 1) {return 0 ;} else if (n = 2) {return 1;} else {return getNumber (n-1) + getNumber (n-2 );}}}

 

Factorial
Package ch07; public class Factorial {// Factorial public static int getFactorialNum (int n) {if (n = 1 | n = 0) {return 1 ;} else {return n * getFactorialNum (n-1 );}}}

 

Fibonacci Series

Assume that an animal has a special species that begins to breed offspring at a rate of 1 per day after being born two days later. Suppose there is an animal like this in the first day (the animal was born and has been breeding offspring since the second day ). So how many days are there?

① Boundary condition: at least one initial definition is non-recursive. F (0) = 0; F (1) = 1. ② Recursive general formula: the unknown function value is calculated gradually from the known function value. F (0) = 0; F (1) = 1, and F (2) = 1 can be calculated, finally, we can calculate the end of F (n.
Package ch07; public class Fibonacci {/*** Implementation of the Fibonacci series 0, 1, 2, 3, 5, 8, 13, 21 ...... ** @ param day */public long maid (int day) {if (day = 0) {// F (0) = 0 return 0 ;} else if (day = 1 | day = 2) {// F (1) = 1 return 1;} else {return fibonacci (day-1) + maid (day-2); // F (n) = F (n-1) + F (n-2 )}} /*** simpler syntax * @ param day * @ return */public long fib (int day) {return day = 0? 0: (day = 1 | day = 2? 1: fib (day-1) + fib (day-2);} // test public static void main (String [] args) {maid = new maid (); system. out. println ("11th days:" + maid. fib (11); System. out. println ("11th days:" + maid. fibonacci (11 ));}}

 

Tower of Hanoi Problems

The game was invented by the mathematician Edward Lucas in 1883. The rules of the game are as follows: There are three fine columns (A, B, C), and six disks are mounted on column, the sizes of the disks are different. They are placed from the largest to the smallest order. Now we need to move all the disks on the column to Column B, and there are the following Conventions when moving them:

Only one disc at the top of the column can be moved at a time. The disc cannot be enlarged on the disc
Package ch07; public class HanoiTower {/*** mobile plate * topN: number of mobile plates * from: Starting tower * inter: middle tower *: target tower */public static void doTower (int topN, char from, char inter, char to) {if (topN = 1) {System. out. println ("plate 1, from" + from + "Tower to" + to + "Tower");} else {doTower (topN-1, from, to, inter ); system. out. println ("plate" + topN + ", from" + from + "Tower to" + to + "Tower"); doTower (topN-1, inter, from, to) ;}} public class TestHanoiTower {public static void main (String [] args) {HanoiTower. doTower (5, 'A', 'B', 'C ');}}

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.