跳台阶动态规划c++
时间: 2023-11-26 08:48:20 浏览: 54
以下是跳台阶动态规划的C++代码实现:
```c++
#include <iostream>
using namespace std;
int jumpFloor(int number) {
if (number <= 0) {
return 0;
} else if (number == 1) {
return 1;
} else if (number == 2) {
return 2;
} else {
int a = 1, b = 2, c;
for (int i = 3; i <= number; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
}
int main() {
int n = 10;
cout << "跳上" << n << "级台阶的跳法总数为:" << jumpFloor(n) << endl;
return 0;
}
```
相关问题
青蛙跳台阶递归算法c++
以下是使用C++递归算法解决青蛙跳台阶问题的示例代码:
```cpp
#include <iostream>
using namespace std;
int jumpFloor(int n) {
if (n <= 0) {
return 1;
} else if (n == 1) {
return 1;
} else if (n == 2) {
return 2;
} else {
return jumpFloor(n - 1) + jumpFloor(n - 2);
}
}
int main() {
int n = 7;
int result = jumpFloor(n);
cout << "跳上 " << n << " 级台阶的跳法总数为:" << result << endl;
return 0;
}
```
运行结果:
```
跳上 7 级台阶的跳法总数为:21
```
给我变态跳台阶的c++代码
以下是变态跳台阶的C++代码:
```c++
#include <iostream>
using namespace std;
int jumpFloorII(int number) {
if (number <= 0) {
return 0;
}
if (number == 1) {
return 1;
}
int sum = 1;
while (number > 1) {
sum *= 2;
number--;
}
return sum;
}
int main() {
int n = 5;
cout << "跳上" << n << "级台阶有" << jumpFloorII(n) << "种跳法" << endl;
return 0;
}
```
其中,jumpFloorII函数用于计算变态跳台阶的跳法数量,n为台阶数。
阅读全文