Last active
October 18, 2017 14:47
-
-
Save iansrc0811/7bfe45df7143ed341e710199fd9cb93c to your computer and use it in GitHub Desktop.
ruby 費式數列(fibonacci), 使用動態規劃法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 複雜度 O(n) | |
input = gets | |
def fib_dy(n, f = [0, 1]) | |
if n >= f.length | |
for i in f.length..n | |
f[i] = f[i-1] + f[i-2] | |
end | |
end | |
return f[n] | |
end | |
puts fib_dy(input.to_i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment