1) 1, 1, 2, 3, 5, 8 ...... use a recursive algorithm to calculate the value of the 30th-digit number?
First we can find from 3rd digits after a digit is equal to the sum of the first two digits, namely: x = (x-1) + (X-2), x> 2;
Here we need to constantly add the values. At the first moment, we will think of loop processing. We try to load these values with arrays, that is:
Int [] A = new int [30];
A [0] = 1;
A [1] = 1;
For (INT I = 2; I <30; I ++)
{
A [I] = A [I-1] + A [I-2];
}
The value of a [29] is the value of the 30th-digit number.
What should we do with recursion? Same definition function
Fun (N)
{
Return fun (n-1) + fun (n-2) // n is the number of digits, and the return value of nth digit is equal to the sum of the value of nth digit and number of digits of N-2
}
Just when n> 2 is like this, we can make an inference.
Fun (N)
{
If (n = 1 | n = 2)
Return 1;
Else
Return fun (n-1) + fun (n-2 );
}
Fun (30 );
The analysis by others on the site is also good:
[Problem] compile the nth function fib (n) for calculating the Fibonacci series ).
Fibonacci series: 0, 1, 1, 2, 3 ,......, That is:
FIB (0) = 0;
FIB (1) = 1;
FIB (n) = fib (n-1) + fib (n-2) (when n> 1 ).
Recursive functions:
Int fib (int n)
{If (n = 0) return 0;
If (n = 1) return 1;
If (n> 1) return fib (n-1) + fib (n-2 );
}
The running process of recursive algorithms is divided into two stages: recursive and regression. In the recurrence stage, the solution of more complex problems (scale N) is pushed to the solution of problems that are simpler than the original problem (scale less than N. For example, in the above example, solve fib (N) and push it to solve fib (n-1) and FIB (n-2 ). That is to say, for calculation of Fib (N), FIB (n-1) and FIB (n-2) must be calculated first, while fib (n-1) and FIB (n-2) must be calculated ), the FIB (n-3) and FIB (n-4) must be calculated first ). And so on until the calculation of Fib (1) and FIB (0) can immediately obtain results 1 and 0, respectively. In the recurrence stage, termination of recursion is required. For example, in function fib, when n is 1 and 0.
In the regression phase, after obtaining the solution of the simplest case, return the result step by step and obtain the solution of a slightly complex problem, for example, after obtaining fib (1) and FIB (0, the result of Fib (2) is returned ,......, After obtaining the results of Fib (n-1) and FIB (n-2), the results of Fib (n) are returned.
When writing recursive functions, note that the knowledge of local variables and counts in the functions is limited to the current call layer. When recursion enters the simple problem layer, the number of shards and local variables at the original level are hidden. In a series of "simple problem" layers, each has its own number of workers and local variables.
Because recursion causes a series of function calls and may involve a series of repeated computations, the running efficiency of recursive algorithms is relatively low. When a recursive algorithm can be easily converted into a recursive algorithm, code is usually compiled based on the recursive algorithm. For example, in the above example, the function fib (n) for calculating the nth item of the Fibonacci series should use a recursive algorithm, that is, starting from the first two items of the Fibonacci series, calculate the next item from the first two items until the required n items are calculated.
Other recursive solutions:
Evaluate the value of 1 + 2 + 3 + 4 + 5 +... + n
Fun (n) = N + fun (n-1)
1 for n = 1
Fun (N)
{
If (n = 1)
Return 1;
Else
Return N + fun (n-1 );
}
There are two integer arrays arranged from small to large. Compile an algorithm to merge them into an array and arrange them from small to large.
Public void fun ()
{
Int [] A = {1, 3, 5, 7, 9, 10 };
Int [] B = {2, 4, 6, 8, 11, 12, 15 };
Int [] C = new int [A. Length + B. Length];
Arraylist Al = new arraylist ();
Int I = 0;
Int J = 0;
While (I <= A. Length-1 & J <= B. Length-1)
{// Put a smaller one in front of a loop than a loop
If (A [I] <B [J])
{
Al. Add (A [I ++]);
}
Else
{
Al. Add (B [J ++]);
}
}
// The length of the two arrays is different, and an array is not complete.
While (I <= A. Length-1) // Add? The remaining
{
Al. Add (A [I ++]);
}
While (j <= B. Length-1) // Add? The rest of B
{
Al. Add (B [J ++]);
}
For (int ii = 0; II <= C. Length-1; II ++)
{
C [II] = (INT) al [II];
}
}
C # Recursive Algorithms