fork download
  1. coins = [5,3,1]
  2. maxcoin = coins.first
  3. base = [0]
  4. diff = 0
  5.  
  6. until diff == maxcoin
  7. number = base.length
  8. min = 1000000
  9. found = false
  10. for c in coins
  11. num = number - c
  12. if num < 0
  13. next
  14. end
  15. dist = 1 + base[num]
  16. if !found || dist < min
  17. found = true
  18. min = dist
  19. end
  20. end
  21. base << min
  22.  
  23. if number >= 2 * maxcoin
  24. sumhi = base[-maxcoin, maxcoin].reduce(:+)
  25. sumlo = base[-maxcoin - maxcoin, maxcoin].reduce(:+)
  26. diff = sumhi - sumlo
  27. end
  28. end
  29.  
  30.  
  31.  
  32. #File.open(ARGV[0]).each_line do |line|
  33. for number in 10000 .. 10009
  34. #number = line.to_i
  35. init = 0;
  36. if number >= base.length
  37. init = 1 + (number - base.length) / maxcoin
  38. end
  39. coins = init + base[number - init * maxcoin]
  40. puts "#{number} -> #{coins}"
  41. end
  42.  
Success #stdin #stdout 0.02s 7464KB
stdin
Standard input is empty
stdout
10000 -> 2000
10001 -> 2001
10002 -> 2002
10003 -> 2001
10004 -> 2002
10005 -> 2001
10006 -> 2002
10007 -> 2003
10008 -> 2002
10009 -> 2003