LoginSignup
0
0

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

Posted at

はじめに

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

A - Six Characters

a-251.rb
s = gets.chomp
puts s * (6 / s.size)

B - At Most 3 (Judge ver.)

b-251.rb
_, w = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)

puts [*1..3].map{|i| a.combination(i).map(&:sum) }.flatten.uniq.count{ |weight_sum| weight_sum <= w}

解説

問題文の通りに、要素から1~3個それぞれ取得しその合計がw以下となるものを求めることで解くことができます。

C - Poem Online Judge

c-251.rb
n = gets.to_i
array = Array.new(n){ gets.split }

hash = {}
array.each_with_index do |(str, score), index|
  next if hash[str]
  hash[str] = [index + 1, score.to_i]
end
puts hash.values.max_by{ |index, score| score }.first

解説

問題文の通りに、オリジナルの提出のうち得点が最大のもの(複数あれば最も提出が早いもの)が何番目か求めることで解くことができます。

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