Files
hello-algo/ru/codes/c/chapter_dynamic_programming/climbing_stairs_dp.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

51 lines
1.6 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: climbing_stairs_dp.c
* Created Time: 2023-09-19
* Author: huawuque404 (huawuque404@163.com)
*/
#include "../utils/common.h"
/* Подъем по лестнице: динамическое программирование */
int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// Инициализация таблицы dp для хранения решений подзадач
int *dp = (int *)malloc((n + 1) * sizeof(int));
// Начальное состояние: заранее задать решения наименьших подзадач
dp[1] = 1;
dp[2] = 2;
// Переход состояний: постепенное решение больших подзадач через меньшие
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
int result = dp[n];
free(dp);
return result;
}
/* Подъем по лестнице: динамическое программирование с оптимизацией памяти */
int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
/* Driver Code */
int main() {
int n = 9;
int res = climbingStairsDP(n);
printf("Количество способов подняться по лестнице из %d ступеней: %d\n", n, res);
res = climbingStairsDPComp(n);
printf("Количество способов подняться по лестнице из %d ступеней: %d\n", n, res);
return 0;
}