0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「第9回オフラインリアルタイムどう書く」のバス代の問題をRubyで解いてみた

Last updated at Posted at 2013-04-12
yhpg009.rb
def discount_sub(map, type, a)
  if a > map[type]
    a -= map[type]
    map[type] = 0
    a
  else
    map[type] -= a
    0
  end
end
def discount(map)
  a = (map["An"]+map["Aw"]+map["Ap"]) * 2
  a = discount_sub(map, "In", a)
  discount_sub(map, "Iw", a)
end
def half(x)
  (x+1) / 2
end
def fee(map, u)
  u * map["An"] +
    half(u) * (map["Aw"]+map["Cn"]+map["In"]) +
    half(half(u)) * (map["Cw"]+map["Iw"])
end
def calculate(line)
  map = Hash.new(0)
  (unit, members) = line.split("0:")
  members.split(",").each{|x| map[x] += 1}
  discount(map)
  fee(map, unit.to_i)*10
end
while line=gets
  puts calculate(line.chomp!)
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?