0
1

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 ABC307(A, B, D)を解いてみた

Posted at

はじめに

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

A - Weekly Records

a-307.rb
gets
a = gets.split.map(&:to_i)
a.each_slice(7){ puts _1.sum }

解説

each_sliceメソッドを使うことで、与えられた配列を7つごとに取得することができます。

B - racecar

b-307.rb
array = Array.new(gets.to_i){ gets.chomp }
puts array.permutation(2).any?{ _1 + _2 == (_1 + _2).reverse } ? "Yes": "No"

解説

permutationメソッドを使って全探索することで、判定することができます。

D - Mismatched Parentheses

d-307.rb
gets
s = gets.chomp.chars

ans = []
checker = []
s.each do
  if _1 == "("
    ans << _1
    checker << ans.size - 1
  elsif _1 == ")" && !checker.empty?
    ans = ans[...checker.pop]
  else
    ans << _1
  end
end
puts ans.join

解説

公式解説を参考にしました

0
1
4

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?