C # recursion,

Source: Internet
Author: User

C # recursion,

What is recursion?

Let's look at the language example. There was a mountain, a temple in the mountains, and an old monk in the temple. He was telling a story to the monk! What is the story? "There was a mountain, a temple in the mountains, and an old monk in the temple. He was telling a story to the monk! What is the story? 'I used to have a mountain, a temple in the mountains, and an old monk in the temple. I am telling a story to the little monk! What is the story ?...... '"

A dog came to the kitchen and stole a small piece of bread. The cook raised his nephew and killed the dog. As a result, all the dogs ran, dug a grave for the dog, and engraved the inscription on the tombstone so that the future dog could see: "A dog came to the kitchen, steal a small piece of bread. The cook raised his nephew and killed the dog. As a result, all the dogs ran, dug a grave for the dog, and engraved the inscription on the tombstone, so that the future dog could see: 'A dog came to the kitchen, steal a small piece of bread. The cook raised his nephew and killed the dog. As a result, all the dogs ran, dug a grave for the dog, and engraved the inscription on the tombstone so that the future dogs could see it ...... '"

See it.

Let's take a look at the mathematical example,

The Fibonacci series is a typical recursive case:

F0 = 0; F = 1; FN = Fn-1 + Fn-2

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144,233 ......

Note that 0 is not the first, but 0th.

 

Let's take a look at programming ideas.

In programming languages, we can think of ourselves as method calls and constant calls. The call is stopped until a condition is reached.

 

Code on

Namespace recursive algorithm {class Program {public static void Main (string [] args) {// output, 55,89 // silk embedding algorithm int [] countNumber = new int [10]; for (int I = 0; I <countNumber. length; I ++) {if (I = 0) {countNumber [0] = 1;} else if (I = 1) {countNumber [1] = 2 ;} if (I> = 2) {countNumber [I] = countNumber [I-1] + countNumber [I-2];} foreach (var Num in countNumber) {Console. write (Num + "");} Console. writeLine ("\ n ------------- I am a split line -------------"); // literary algorithm, recursive int [] countNumber2 = new int [10]; for (int I = 0; I <countNumber2.Length; I ++) {countNumber2 [I] = DiGui (I);} foreach (var Num2 in countNumber2) {Console. write (Num2 + "");} Console. readKey ();} public static int DiGui (int j) {int s; if (j = 0 | j = 1) {s = j + 1 ;} else {s = DiGui (J-1) + DiGui (J-2); // call method itself} return s ;}}}

Portal: recursive classical algorithm

Portal: Why is recursive algorithms so slow?

 

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.