一天一只顽猴想要从山脚爬到山顶,
途中经过一个有n
个台阶的阶梯,
但是这个猴子有个习惯,每一次只跳1
步或3
步
试问?猴子通过这个阶梯有多少种不同的跳跃方式
输入只有一个数n
, 0 < n < 50
代表此阶梯有多个台阶
有多少种跳跃方式
50
122106097
3
2
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Author: Amos
* E-mail: amos@amoscloud.com
* Date: 2020/11/2
* Time: 16:41
* Description:
*/
public class Main0004 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int n = scanner.nextInt();
solution(n);
}
}
private static void solution(int n) {
int step1 = 1, step2 = 1, step3 = 2;
int step4 = n == 1 || n == 2 ? 1 : 2;
for (int i = 4; i <= n; i++) {
step4 = step3 + step1;
step1 = step2;
step2 = step3;
step3 = step4;
}
System.out.println(step4);
}
}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created with IntelliJ IDEA.
File: main0004
Author: Amos
E-mail: amos@amoscloud.com
Date: 2023/2/8
Time: 21:08
Description:
"""
n = int(input())
steps = [1, 1, 2]
res = 1 if n == 1 or n == 2 else 2
for i in range(4, n + 1):
res = steps[2] + steps[0]
steps = steps[1:] + [res]
print(res)