mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -8,12 +8,12 @@
|
||||
|
||||
/* Recursion */
|
||||
int recur(int n) {
|
||||
// Termination condition
|
||||
// Base case
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// Recurse: recursive call
|
||||
// Recurse: make a recursive call
|
||||
int res = recur(n - 1);
|
||||
// Return: return result
|
||||
// Unwind: return the result
|
||||
return n + res;
|
||||
}
|
||||
|
||||
@@ -22,14 +22,14 @@ int forLoopRecur(int n) {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
stack<int> stack;
|
||||
int res = 0;
|
||||
// Recurse: recursive call
|
||||
// Recurse: make a recursive call
|
||||
for (int i = n; i > 0; i--) {
|
||||
// Simulate "recurse" with "push"
|
||||
// Simulate recursive calls by pushing onto the stack
|
||||
stack.push(i);
|
||||
}
|
||||
// Return: return result
|
||||
// Unwind: return the result
|
||||
while (!stack.empty()) {
|
||||
// Simulate "return" with "pop"
|
||||
// Simulate unwinding by popping from the stack
|
||||
res += stack.top();
|
||||
stack.pop();
|
||||
}
|
||||
@@ -39,40 +39,40 @@ int forLoopRecur(int n) {
|
||||
|
||||
/* Tail recursion */
|
||||
int tailRecur(int n, int res) {
|
||||
// Termination condition
|
||||
// Base case
|
||||
if (n == 0)
|
||||
return res;
|
||||
// Tail recursive call
|
||||
// Tail-recursive call
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: recursion */
|
||||
int fib(int n) {
|
||||
// Termination condition f(1) = 0, f(2) = 1
|
||||
// Base cases: f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
return n - 1;
|
||||
// Recursive call f(n) = f(n-1) + f(n-2)
|
||||
// Recursive calls: f(n) = f(n-1) + f(n-2)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// Return result f(n)
|
||||
// Return the result f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
cout << "\nRecursive function sum result res = " << res << endl;
|
||||
cout << "\nSum from the recursive function, res = " << res << endl;
|
||||
|
||||
res = forLoopRecur(n);
|
||||
cout << "\nUsing iteration to simulate recursive sum result res = " << res << endl;
|
||||
cout << "\nSum from simulating recursion with iteration, res = " << res << endl;
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
cout << "\nTail recursive function sum result res = " << res << endl;
|
||||
cout << "\nSum from the tail-recursive function, res = " << res << endl;
|
||||
|
||||
res = fib(n);
|
||||
cout << "\nThe " << n << "th term of the Fibonacci sequence is " << res << endl;
|
||||
cout << "\nFibonacci term " << n << " is " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user