Recursive algorithm,
A recursive algorithm is an algorithm that directly or indirectly calls its own functions or methods. (Call yourself)
Recursion principle:
1. The function will always call itself until a specific condition is met (there must be an end condition for recursion). 2. parameters will be passed during recursion, each call passes a new parameter to itself. For example, use a recursive algorithm to calculate the sum between 1 and 100. The Code is as follows:
1 public class Test 2 {3 void Start () 4 {5 int sum = AddNum (100); 6 print (sum ); // sum = 5050 7} 8 9 // This method is constantly calling itself until n = 1, and then continuously returning the return value to the previous layer, until the outermost layer. 10 private int AddNum (int n) 11 {12 if (n = 1) 13 {14 return 1; 15} 16 return n + AddNum (n-1 ); 17} 18}