AOJ(問題集)3
AIZU ONLINE JUDGE: Programming Challenge
0021 Parallelism
$<.readlines.drop(1).map {|a| a.split.map(&:to_r)}.each do |x1, y1, x2, y2, x3, y3, x4, y4| puts((x2 - x1) * (y4 - y3) == (x4 - x3) * (y2 - y1) ? "YES" : "NO") end
単純な問題なのに、自分の解き方ではどうしてもダメだったので、他の人のを参考に。to_f
じゃなくて to_r
がミソなのかなあ。
0022 Maximum Sum Sequence
while (n = $<.gets.to_i).nonzero? given = (1..n).map {$<.gets.to_i} max = -Float::INFINITY n.times do |i| sum = 0 (i...n).each do |j| sum += given[j] max = sum if sum > max end end puts max end
0023 Circles Intersection
$<.readlines.drop(1).map {|a| a.split.map(&:to_f)}.each do |xa, ya, ra, xb, yb, rb| l = Math.sqrt((xb - xa) ** 2 + (yb - ya) ** 2) r = ra - rb puts case when l < r then 2 when l < -r then -2 when l > ra + rb then 0 else 1 end end
0024 Physical Experiments
$<.readlines.map(&:to_r).each do |v| puts((1 + 1/98r * v ** 2).ceil) end
0025 Hit and Blow
$<.readlines.map {|a| a.split.map(&:to_i)}.each_slice(2) do |a, b| a1, b1 = [], [] hit = 0 a.each_index do |i| if a[i] == b[i] hit += 1 else a1 << a[i] b1 << b[i] end end blow = a1.size - (a1 - b1).size puts "#{hit} #{blow}" end
0026 Dropping Ink
L = 10 table = [a = [[0, 0], [0, -1], [1, 0], [0, 1], [-1, 0]], b = a + [[1, -1], [1, 1], [-1, 1], [-1, -1]], b + [[0, -2], [2, 0], [0, 2], [-2, 0]]] field = Array.new(L) {Array.new(L, 0)} set = ->(x, y) { return if x < 0 or x >= L or y < 0 or y >= L field[y][x] += 1 } $<.readlines.map {|l| l.split(",").map(&:to_i)}.each do |x, y, s| table[s - 1].each {|dx, dy| set.(x + dx, y + dy)} end f = field.flatten puts f.count(0) puts f.max
0027 What day is today?
require 'date' table = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday) loop do month, day = $<.gets.split.map(&:to_i) break if month.zero? puts table[Date.new(2004, month, day).wday] end
0028 Mode Value
field = Array.new(101, 0) $<.readlines.map(&:to_i).each {|n| field[n] += 1} m = field.max field.each_with_index {|n, i| puts i if m == n}
0029 English Sentence
counts = Hash.new(0) max = [0, nil] $<.gets.split.map(&:downcase).each do |word| counts[word] += 1 l = word.length max = [l, word] if l > max.first end h = counts.invert puts "#{h[h.keys.max]} #{max.last}"
0030 Sum of Integers
loop do n, s = $<.gets.split.map(&:to_i) break if n.zero? and s.zero? puts [*0..9].combination(n).map {|numbers| numbers.sum == s}.count(true) end