AOJ(問題集)2

AIZU ONLINE JUDGE: Programming Challenge
 

0011 Drawing Lots

given = $<.readlines
lines = [*0..given.first.to_i]
given.drop(2).map {|x| x.split(",").map(&:to_i)}.each do |a, b|
  lines[a], lines[b] = lines[b], lines[a]
end
puts lines.drop(1)

 

0012 A Point in a Triangle

$<.readlines.each do |l|
  x1, y1, x2, y2, x3, y3, xp, yp = l.split.map(&:to_f)
  xa, ya = xp - x1, yp - y1
  xb, yb = x3 - x1, y3 - y1
  xc, yc = x2 - x3, y2 - y3
  r = xb * yc - xc * yb
  s = (yc * xa - xc * ya) / r
  u = (xb * ya - yb * xa) / r
  t = u / s
  puts((0 < s and s < 1 and 0 < t and t < 1) ? "YES" : "NO")
end

 

0013 Switching Railroad Cars

stack = []
while (n = $<.gets)
  n = n.to_i
  if n.zero?
    puts stack.pop
  else
    stack << n
  end
end

 

0014 Integral

L = 600
$<.readlines.map(&:to_i).each do |d|
  puts (1..L / d - 1).map {|i| (i * d) ** 2 * d}.sum
end

 

0015 National Budget

$<.readlines.drop(1).map(&:to_i).each_slice(2) do |a, b|
  st = (a + b).to_s
  puts((st.length <= 80) ? st : "overflow")
end

 

0016 Treasure Hunt

x, y = 0, 0
θ = 90
loop do
  given = $<.gets.split(",").map(&:to_i)
  break if given == [0, 0]
  x += given[0] * Math.cos(Math::PI * θ / 180)
  y += given[0] * Math.sin(Math::PI * θ / 180)
  θ -= given[1]
end
puts x.to_i
puts y.to_i

 

0017 Caesar Cipher

def decode(st, n)
  trans = ->(b) {(a = b - n) < 97 ? a + 26 : a}
  st.bytes.map {|b| (b.between?(97, 122) ? trans.(b) : b).chr}.join
end

words = %w(the this that)
table = (0..25).map do |i|
  words.map {|w| w.bytes.map {|b| ((a = b + i) > 122 ? a - 26 : a).chr}.join}
end

$<.readlines.each do |st|
  st.chomp!
  n = 0
  table.each_with_index do |ws, i|
    ws.each {|w| n = i if st.include?(w)}
  end
  puts decode(st, n)
end

 

0018 Sorting Five Numbers

puts $<.gets.split.map(&:to_i).sort {|a, b| b <=> a}.join(" ")

 

0019 Factorial

puts (1..$<.gets.to_i).inject(&:*)

 

0020 Capitalize

puts $<.gets.chomp.upcase