70.爬楼梯

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 12 个台阶。你有多少种不同的方法可以爬到楼顶呢?

思路

  • 爬到第n阶的方法,就是爬到n-1+n-2阶的方法的和
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.wereash.scut_hot100;

import java.util.Scanner;

/**
* @Author: WereAsh
* @Date:2026-01-27 19:05
**/
public class Solution070 {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
if(n==1){
System.out.println("共有1种不同的爬法");
return;
}
int[] dp=new int[n+1];
dp[0]=1;
dp[1]=1;
for (int i=2;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
System.out.println("共有"+dp[n]+"种不同的爬法");
}
}