Files
hello-algo/ru/codes/c/chapter_computational_complexity/recursion.c
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

78 lines
2.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* File: recursion.c
* Created Time: 2023-09-09
* Author: Gonglja (glj0@outlook.com)
*/
#include "../utils/common.h"
/* Рекурсия */
int recur(int n) {
// Условие завершения
if (n == 1)
return 1;
// Рекурсия: рекурсивный вызов
int res = recur(n - 1);
// Возврат: вернуть результат
return n + res;
}
/* Имитация рекурсии итерацией */
int forLoopRecur(int n) {
int stack[1000]; // Использовать большой массив для имитации стека
int top = -1; // Индекс вершины стека
int res = 0;
// Рекурсия: рекурсивный вызов
for (int i = n; i > 0; i--) {
// Имитировать «рекурсию» с помощью операции помещения в стек
stack[1 + top++] = i;
}
// Возврат: вернуть результат
while (top >= 0) {
// Имитировать «возврат» с помощью операции извлечения из стека
res += stack[top--];
}
// res = 1+2+3+...+n
return res;
}
/* Хвостовая рекурсия */
int tailRecur(int n, int res) {
// Условие завершения
if (n == 0)
return res;
// Хвостовой рекурсивный вызов
return tailRecur(n - 1, res + n);
}
/* Последовательность Фибоначчи: рекурсия */
int fib(int n) {
// Условие завершения: f(1) = 0, f(2) = 1
if (n == 1 || n == 2)
return n - 1;
// Рекурсивный вызов f(n) = f(n-1) + f(n-2)
int res = fib(n - 1) + fib(n - 2);
// Вернуть результат f(n)
return res;
}
/* Driver Code */
int main() {
int n = 5;
int res;
res = recur(n);
printf("\nРезультат суммирования в рекурсивной функции res = %d\n", res);
res = forLoopRecur(n);
printf("\nРезультат суммирования с использованием итерации для имитации рекурсии res = %d\n", res);
res = tailRecur(n, 0);
printf("\nРезультат суммирования в хвостовой рекурсии res = %d\n", res);
res = fib(n);
printf("\nЭлемент последовательности Фибоначчи с индексом %d = %d\n", n, res);
return 0;
}