Skip to content

Instantly share code, notes, and snippets.

@iansrc0811
Last active October 18, 2017 14:47
Show Gist options
  • Save iansrc0811/7bfe45df7143ed341e710199fd9cb93c to your computer and use it in GitHub Desktop.
Save iansrc0811/7bfe45df7143ed341e710199fd9cb93c to your computer and use it in GitHub Desktop.
ruby 費式數列(fibonacci), 使用動態規劃法
# 複雜度 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