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?