mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
27 lines
534 B
Ruby
27 lines
534 B
Ruby
=begin
|
|
File: climbing_stairs_dfs.rb
|
|
Created Time: 2024-05-29
|
|
Author: Xuan Khoa Tu Nguyen ([email protected])
|
|
=end
|
|
|
|
### 探索 ###
|
|
def dfs(i)
|
|
# dp[1] と dp[2] は既知なので、その値を返す
|
|
return i if i == 1 || i == 2
|
|
# dp[i] = dp[i-1] + dp[i-2]
|
|
dfs(i - 1) + dfs(i - 2)
|
|
end
|
|
|
|
### 階段の上り方:探索 ###
|
|
def climbing_stairs_dfs(n)
|
|
dfs(n)
|
|
end
|
|
|
|
### 実行用コード ###
|
|
if __FILE__ == $0
|
|
n = 9
|
|
|
|
res = climbing_stairs_dfs(n)
|
|
puts "#{n} 段の階段を上る方法は全部で #{res} 通り"
|
|
end
|