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

Last updated at Posted at 2023-05-10

はじめに

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

A - Full House

a-263.rb
a = gets.split.sort
puts (a[0] == a[2] && a[3] == a[4]) || (a[0] == a[1] && a[2] == a[4])  ? "Yes" : "No"

解説

ソートを使うことで、3枚の部分は真ん中のカードの探索を省略することができます。

B - Ancestor

b-263.rb
n = gets.to_i
m = gets.split.map(&:to_i)
ans, i = 1, n - 2
while m[i] != 1
  i = m[i] - 2
  ans += 1
end
puts ans

解説

人Nから人1になるまで探索していくことで実装することができます。

C - Monotonically Increasing

c-263.rb
n, m = gets.split.map(&:to_i)
- (1..m).to_a.combination(n).each do |array|
+ (1..m).to_a.combination(n) do |array|
    puts array.join(" ")
  end

解説

N, Mともに10以下と小さいため、combinationメソッドを使った全探索により実装することができます。

0
0
2

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?