Last active
November 19, 2015 02:07
-
-
Save shoken0x/387777b55e8f688fe302 to your computer and use it in GitHub Desktop.
PCC Book: 貪欲法 Greedy algorithm
This file contains 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
#input | |
$V = [1, 5, 10, 50, 100, 500] | |
# 1円玉3枚, 10円玉2枚... 500円玉2枚 | |
$C = [3, 2, 1, 3, 0, 2] | |
$A = 620 #620円支払う | |
# $A円を支払う最小の硬貨数を求める | |
def slove | |
ans = 0 | |
5.downto(0) do |i| | |
t = [$A / $V[i], $C[i]].min | |
$A -= t * $V[i] | |
ans += t | |
end | |
puts ans | |
end | |
slove |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment