先週のつづき
先週に続いてACM/ICPC国内予選突破の手引きの問題をRubyでといてみる。
Exploring Caves
# # exploringcaves.rb class KarakuriDoll def initialize @x = @y = @farthest_x = @farthest_y = 0 end attr_reader :farthest_x attr_reader :farthest_y def move(x, y) @x += x @y += y if (@x**2 + @y**2) > (@farthest_x**2 + @farthest_y**2) @farthest_x, @farthest_y = @x, @y elsif (@x**2 + @y**2) == (@farthest_x**2 + @farthest_y**2) && @x > @farthest_x @farthest_x, @farthest_y = @x, @y end end end def main results = [] readline.to_i.times do doll = KarakuriDoll.new loop do dx, dy = readline.split(/\s/).map{|n| n.to_i } break if dx == 0 and dy == 0 doll.move dx, dy end results << [doll.farthest_x, doll.farthest_y] end results.each do |res| puts "#{res[0]} #{res[1]}" end end main
When Can We Meet?
# 2028 # When Can We Meet? class BallotBox def initialize @vote = {} end def ballot(days) days.each do |d| if @vote[d].nil? @vote[d] = 1 else @vote[d] = @vote[d] + 1 end end end def winner win = 0 vote = 0 @vote.each do |d, v| if v > vote or (v == vote and win > d) win , vote = d, v end end [win, vote] end end def main results = [] loop do n, q = readline.split(/\s/).map{|n| n.to_i } break if n == 0 and q == 0 box = BallotBox.new n.times do box.ballot(readline.split(/\s/)[1..-1].map{|n| n.to_i }) end win, vote = box.winner results << (vote >= q ? win : 0) end results end puts main.join("\n")