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

Posted at

はじめに

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

A - Water Pressure

a-231.rb
puts gets.to_i.fdiv(100)

B - Election

b-231.rb
n = gets.to_i
hash = Array.new(n){ gets.chomp }.tally
puts hash.key(hash.values.max)

解説

tallyメソッドで同じkey同士をくくった後のvalueが最大となるkeyが答えとなります。(max_byを使った書き方もできると思います)

C - Counting 2

c-231.rb
n, q = gets.split.map(&:to_i)
a = gets.split.map(&:to_i).sort
q.times do
  x = gets.to_i
  i = a.bsearch_index{ _1 >= x } || n
  puts n - i
end

解説

bsearchメソッドを使って二分探索することで、それぞれのXに対してXよりも大きい要素数を求めることができます。
※ countメソッドだとTLEしてしまいます

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?