AOJ (Introduction to Programming)

AIZU ONLINE JUDGE: Programming Challenge

ITP1_1_A

puts "Hello World"

ITP1_1_B

x = gets.to_i
puts x ** 3

ITP1_1_C

a, b = gets.split.map(&:to_i)
puts "#{a * b} #{2 * (a + b)}"

ITP1_1_D

n = gets.to_i
puts "#{n / 3600}:#{(n % 3600) / 60}:#{n % 60}"

 
ITP1_2_A

a, b = gets.split.map(&:to_i)
st = if    a < b then "<"
     elsif a > b then ">"
     else "=="
     end
puts "a #{st} b"

ITP1_2_B

a, b, c = gets.split.map(&:to_i)
puts (a < b and b < c) ? "Yes" : "No"

ITP1_2_C

puts gets.split.map(&:to_i).sort.join(" ")

ITP1_2_D

w, h, x, y, r = gets.split.map(&:to_i)
st = if x - r >= 0 and y - r >= 0 and x + r <= w and y + r <= h
  "Yes"
else
  "No"
end
puts st

 
ITP1_3_A

1000.times {puts "Hello World"}

IPT1_3_B

i = 0
until (x = gets.to_i).zero?
  puts "Case #{i += 1}: #{x}"
end

ITP1_3_C

loop do
  x, y = gets.split.map(&:to_i).sort
  break if x.zero? and y.zero?
  puts "#{x} #{y}"
end

ITP1_3_D

a, b, c = gets.split.map(&:to_i)
cnt = 0
a.upto(b) {|i| cnt += 1 if (c % i).zero?}
puts cnt

 
ITP1_4_A

a, b = gets.split.map(&:to_i)
printf "%d %d %.5f", a / b, a % b, a.to_f / b

ITP1_4_B

r = gets.to_f
pi = Math::PI
printf "%.6f %.6f", pi * r ** 2, 2 * pi * r

ITP1_4_C

until /\d+ \? \d+/.match(st = gets)
  puts eval(st)
end

ITP1_4_D

gets
a = gets.split.map(&:to_i)
puts "#{a.min} #{a.max} #{a.inject(&:+)}"

 
ITP1_5_A

loop do
  h, w = gets.split.map(&:to_i)
  break if h.zero? and w.zero?
  h.times {puts "#" * w}
  puts
end

ITP1_5_B

loop do
  h, w = gets.split.map(&:to_i)
  break if h.zero? and w.zero?
  st = "#" * w
  puts st
  (h - 2).times {puts "#" + "." * (w - 2) + "#"}
  puts st
  puts
end

ITP1_5_C

loop do
  h, w = gets.split.map(&:to_i)
  break if h.zero? and w.zero?
  st1 = ("#." * w)[0, w]
  st2 = ("." + st1)[0, w]
  h.times {|i| puts [st1, st2][i % 2]}
  puts
end

ITP1_5_D

n = gets.to_i

i = 1
while i <= n
  x = i
  if (x % 3).zero?
    print " #{i}"
  else
    until x.zero?
      if x % 10 == 3
        print " #{i}"
        break
      else
        x /= 10
      end
    end
  end
  i += 1
end
puts

 
ITP1_6_A

gets
puts gets.split.reverse.join(" ")

ITP1_6_B

n = gets.to_i
cards = []
%W(S H C D).each do |suit|
  1.upto(13) {|i| cards << "#{suit} #{i}"}
end
n.times {cards -= [gets.chomp]}
puts cards

ITP1_6_C

n = gets.to_i
rooms = Array.new(4) {Array.new(3) {Array.new(10, 0)}}
n.times do
  b, f, r, v = gets.split.map(&:to_i)
  rooms[b - 1][f - 1][r - 1] += v
end
st = Array.new(4, "")
4.times do |b|
  3.times do |f|
    10.times do |r|
      st[b] += " " + rooms[b][f][r].to_s
    end
    st[b] += "\n"
  end
end
print st.join("#" * 20 + "\n")

ITP1_6_D

n, m = gets.split.map(&:to_i)
a, c = [], []
n.times {a << gets.split.map(&:to_i)}
m.times {c << gets.to_i}

n.times do |i|
  sum = 0
  m.times {|j| sum += a[i][j] * c[j]}
  puts sum
end

 
ITP1_7_A

loop do
  m, f, r = gets.split.map(&:to_i)
  break if m == -1 and f == -1 and r == -1
  sum = m + f
  st = if m == -1 or f == -1
    "F"
  elsif sum >= 80
    "A"
  elsif sum >= 65
    "B"
  elsif sum >= 50
    "C"
  elsif sum >= 30
    (r >= 50) ? "C" : "D"
  else
    "F"
  end
  puts st
end

ITP1_7_B

loop do
  n, x = gets.split.map(&:to_i)
  break if n.zero? and x.zero?
  cnt = 0
  [*1..n].combination(3) do |ar|
    cnt += 1 if ar[0] + ar[1] + ar[2] == x
  end
  puts cnt
end

ITP1_7_C

r, c = gets.split.map(&:to_i)
m = []
r.times {m << gets.split.map(&:to_i)}
tate = Array.new(c, 0)
putout = ->(ar) {
  st = ""
  sum = 0
  ar.each_with_index do |x, i|
    st += x.to_s + " "
    sum += x
    tate[i] += x
  end
  st += sum.to_s
  puts st
}
m.each {|x| putout.(x)}
putout.(tate)

ITP1_7_D

n, m, l = gets.split.map(&:to_i)
a, b = [], []
n.times {a << gets.split.map(&:to_i)}
m.times {b << gets.split.map(&:to_i)}

a.each do |row|
  c = Array.new(l, 0)
  row.each_with_index do |x, i|
    l.times {|j| c[j] += x * b[i][j]}
  end
  puts c.join(" ")
end

 
ITP1_8_A

st = ""
gets.chomp.each_byte do |byte|
  st += if byte.between?(97, 122)
    (byte - 32).chr
  elsif byte.between?(65, 90)
    (byte + 32).chr
  else
    byte.chr
  end
end
puts st

ITP1_8_B

loop do
  x = gets.chomp
  break if x == "0"
  puts x.chars.map(&:to_i).inject(&:+)
end

ITP1_8_C

result = Array.new(26, 0)
$stdin.readlines.map(&:chomp).join.downcase.each_byte do |byte|
  next unless byte.between?(97, 122)
  result[byte - 97] += 1
end
result.each_with_index {|x, i| puts "#{(i + 97).chr} : #{x}"}

ITP1_8_D

s, p = gets.chomp, gets.chomp
ring = s + s
l = p.length
result = "No"
s.length.times do |i|
  if ring[i, l] == p
    result = "Yes"
    break
  end
end
puts result

 
ITP1_9_A

w = gets.chomp
t = ""
loop do
  st = gets.chomp
  break if st == "END_OF_TEXT"
  t += st.downcase + " "
end

puts t.split.count(w)

ITP1_9_B

loop do
  deck = gets.chomp
  break if deck == "-"
  gets.to_i.times do
    i = gets.to_i
    deck = deck[i..-1] + deck[0, i]
  end
  puts deck
end

ITP1_9_C

taro = hanako = 0
gets.to_i.times do
  t, h = gets.chomp.split
  if t > h
    taro += 3
  elsif t < h
    hanako += 3
  else
    taro += 1
    hanako += 1
  end
end
puts "#{taro} #{hanako}"

ITP1_9_D

str = gets.chomp
gets.to_i.times do
  command = gets.split
  a = command[1].to_i
  b = command[2].to_i
  case command[0]
  when "print"
    puts str[a..b]
  when "reverse"
    str = str[0, a] + str[a..b].reverse + str[b + 1..-1]
  when "replace"
    str = str[0, a] + command[3] + str[b + 1..-1]
  end
end

 
ITP1_10_A

x1, y1, x2, y2 = gets.split.map(&:to_f)
puts Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

ITP1_10_B

include Math

a, b, c = gets.split.map(&:to_i)
θ = c / 180.0 * PI
h = b * sin(θ)

puts a * h / 2
puts a + b + sqrt(a ** 2 + b ** 2 - 2 * a * b * cos(θ))
puts h

ITP1_10_C

#標準偏差
loop do
  n = gets.to_i
  break if n.zero?
  s = gets.split.map(&:to_i)
  m = s.inject(&:+) / n.to_f
  sum = 0
  s.each {|i| sum += (i - m) ** 2}
  puts Math.sqrt(sum / n)
end

ITP1_10_D

n = gets.to_i
x = gets.split.map(&:to_i)
y = gets.split.map(&:to_i)
d = ->(p) {
  sum = 0
  n.times {|i| sum += (x[i] - y[i]).abs ** p}
  sum ** (1 / p.to_f)
}

1.upto(3) {|i| puts d.(i)}
puts x.zip(y).map {|x, y| (x - y).abs.to_f}.max

 
ITP1_11_A

dice = gets.split.map(&:to_i)
commands = gets.chomp.chars.map(&:to_sym)
throw = {E: [3, 2, 6, 1, 5, 4], N: [5, 1, 3, 4, 6, 2],
         S: [2, 6, 3, 4, 1, 5], W: [4, 2, 1, 6, 5, 3]}
th = {}
throw.each_key {|k| th[k] = throw[k].map(&:pred)}

commands.each do |c|
  next_dice = Array.new(6, 0)
  dice.each_with_index do |d, i|
    next_dice[th[c][i]] = d
  end
  dice = next_dice
end
puts dice[0]

ITP1_11_B

possible_appearances = ->(dice) {
  table = 
     [[0, 1, 2, 3, 4, 5], [3, 1, 0, 5, 4, 2], [1, 5, 2, 3, 0, 4], [5, 1, 3, 2, 4, 0],
      [1, 2, 0, 5, 3, 4], [3, 5, 1, 4, 0, 2], [5, 4, 2, 3, 1, 0], [2, 1, 5, 0, 4, 3],
      [1, 0, 3, 2, 5, 4], [5, 2, 1, 4, 3, 0], [2, 4, 0, 5, 1, 3], [4, 5, 3, 2, 0, 1],
      [3, 4, 5, 0, 1, 2], [4, 0, 2, 3, 5, 1], [1, 3, 5, 0, 2, 4], [2, 0, 1, 4, 5, 3],
      [0, 4, 3, 2, 1, 5], [4, 2, 5, 0, 3, 1], [4, 3, 0, 5, 2, 1], [2, 5, 4, 1, 0, 3],
      [3, 0, 4, 1, 5, 2], [0, 3, 1, 4, 2, 5], [0, 2, 4, 1, 3, 5], [5, 3, 4, 1, 2, 0]]
  result = []
  table.each do |dice1|
    another = Array.new(6, 0)
    dice1.each_with_index {|n, i| another[n] = dice[i]}
    result << another
  end
  result.uniq
}

appearances = possible_appearances.(gets.split.map(&:to_i))
gets.to_i.times do
  top, front = gets.split.map(&:to_i)
  appearances.each do |a|
    if a[0] == top and a[1] == front
      puts a[2]
      break
    end
  end
end

ITP1_11_C

possible_appearances = ->(dice) {
  table = 
     [[0, 1, 2, 3, 4, 5], [3, 1, 0, 5, 4, 2], [1, 5, 2, 3, 0, 4], [5, 1, 3, 2, 4, 0],
      [1, 2, 0, 5, 3, 4], [3, 5, 1, 4, 0, 2], [5, 4, 2, 3, 1, 0], [2, 1, 5, 0, 4, 3],
      [1, 0, 3, 2, 5, 4], [5, 2, 1, 4, 3, 0], [2, 4, 0, 5, 1, 3], [4, 5, 3, 2, 0, 1],
      [3, 4, 5, 0, 1, 2], [4, 0, 2, 3, 5, 1], [1, 3, 5, 0, 2, 4], [2, 0, 1, 4, 5, 3],
      [0, 4, 3, 2, 1, 5], [4, 2, 5, 0, 3, 1], [4, 3, 0, 5, 2, 1], [2, 5, 4, 1, 0, 3],
      [3, 0, 4, 1, 5, 2], [0, 3, 1, 4, 2, 5], [0, 2, 4, 1, 3, 5], [5, 3, 4, 1, 2, 0]]
  result = []
  table.each do |dice1|
    another = Array.new(6, 0)
    dice1.each_with_index {|n, i| another[n] = dice[i]}
    result << another
  end
  result.uniq
}

dice1 = gets.split.map(&:to_i)
dice2 = gets.split.map(&:to_i)
puts possible_appearances.(dice1).include?(dice2) ? "Yes" : "No"

ITP1_11_D

possible_appearances = ->(dice) {
  table = 
     [[0, 1, 2, 3, 4, 5], [3, 1, 0, 5, 4, 2], [1, 5, 2, 3, 0, 4], [5, 1, 3, 2, 4, 0],
      [1, 2, 0, 5, 3, 4], [3, 5, 1, 4, 0, 2], [5, 4, 2, 3, 1, 0], [2, 1, 5, 0, 4, 3],
      [1, 0, 3, 2, 5, 4], [5, 2, 1, 4, 3, 0], [2, 4, 0, 5, 1, 3], [4, 5, 3, 2, 0, 1],
      [3, 4, 5, 0, 1, 2], [4, 0, 2, 3, 5, 1], [1, 3, 5, 0, 2, 4], [2, 0, 1, 4, 5, 3],
      [0, 4, 3, 2, 1, 5], [4, 2, 5, 0, 3, 1], [4, 3, 0, 5, 2, 1], [2, 5, 4, 1, 0, 3],
      [3, 0, 4, 1, 5, 2], [0, 3, 1, 4, 2, 5], [0, 2, 4, 1, 3, 5], [5, 3, 4, 1, 2, 0]]
  result = []
  table.each do |dice1|
    another = Array.new(6, 0)
    dice1.each_with_index {|n, i| another[n] = dice[i]}
    result << another
  end
  result.uniq
}

solve = ->(dices) {
  s = dices.size
  (s - 1).times do |i|
    appearances = possible_appearances.(dices[i])
    (i + 1).upto(s - 1) do |j|
      return "No" if appearances.include?(dices[j])
    end
  end
  "Yes"
}

n = gets.to_i
dices = []
n.times {dices << gets.split.map(&:to_i)}
puts solve.(dices)