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 1 year has passed since last update.

RubyでAtCoder ABC236(A, B, C)を解いてみた

Posted at

はじめに

Webエンジニアを目指して、RubyやRailsをいじってます。
今回は、RubyでAtCoder ABC236のA, B, Cを解きました。備忘録として解き方をまとめていきたいと思います。

A - chukodai

a-236.rb
s = gets.chomp
a, b = gets.split.map(&:to_i)
a -= 1
b -= 1
s[a], s[b] = s[b], s[a]
puts s

B - Who is missing?

b-236.rb
gets
puts gets.split.tally.invert[3]

解説

invertメソッドを使ってkeyとvalueを入れ替えています。

C - Route Map

c-236.rb
n, m = gets.split.map(&:to_i)
s = gets.split
t = gets.split

s.each do
  if _1 == t[0]
    t.shift
    puts "Yes"
  else
    puts "No"
  end
end

解説

S1==T1かつSN==TMよりSの先頭の要素から順番にTの先頭の要素と一致するかどうかを調べていき、一致すればTから先頭の要素を削除してYesを出力し、一致しなければNoを出力します。

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?