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?

paizaラーニング解答: 条件分岐メニュー[Ruby]

Posted at

条件分岐メニュー

STEP: 1 単純な条件分岐

s = gets.chomp
puts s == "paiza" ? "YES" : "NO"

STEP: 2 数値の分岐

n = gets.to_i
puts n <= 100 ? "YES" : "NO"

STEP: 3 数値演算結果で分岐

a, b, c = gets.split.map(&:to_i)
puts a * b <= c ? "YES" : "NO"

STEP: 4 ゼロ以外

n = gets.to_i
puts n.nonzero? ? "YES" : "NO"

FINAL問題 0 が含まれていないか判定

n = gets.to_i
a = n.times.map { gets.to_i }
puts a.include?(0) ? "NO" : "YES"

STEP: 1 AND

a, b = gets.split.map(&:to_i)
puts a >= 10 && b >= 10 ? "YES" : "NO"

STEP: 2 大文字判定

c = gets.chomp
puts c == c.upcase ? "YES" : "NO"

cをupcaseで大文字にしたものと元のcが同じであれば、それは大文字。

STEP: 3 OR

a, b = gets.split.map(&:to_i)
puts a >= 10 || b >= 10 ? "YES" : "NO"

STEP: 4 NOT

x = gets.to_i
puts !(x >= 10) ? "YES" : "NO"

STEP: 5 AND+NOT

a, b = gets.split.map(&:to_i)
puts a >= 10 && !(b >= 10) ? "YES" : "NO"

FINAL問題 AND+OR

x, y, z = gets.split.map(&:to_i)
puts (x >= 10 && y >= 10) || z >= 10 ? "YES" : "NO"

STEP: 1 3 の倍数判定

n = gets.to_i
puts n % 3 == 0 ? "YES" : "NO"

STEP: 2 2 つの倍数判定

n = gets.to_i
puts n % 15 == 0 ? "YES" : "NO"

STEP: 3 偶奇の判定

n = gets.to_i
a = gets.split.map(&:to_i)
even = 0
odd = 0
a.each { |i| i.even? ? even += 1 : odd += 1 }
puts "#{even} #{odd}"

STEP: 4 曜日の判定

n = gets.to_i
week = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]
puts week[n % 7]

FINAL問題 FizzBuzz

n = gets.to_i
if n % 15 == 0
    puts "FizzBuzz"
elsif n % 3 == 0
    puts "Fizz"
elsif n % 5 == 0
    puts "Buzz"
else
    puts n
end

FizzBuzz問題はpaizaラーニングの講座で詳しく取り上げられています。講座内で使用するプログラミング言語は違いますが、説明に沿って取り組めば問題ありません。

STEP: 1 けた数の測定

n = gets.chomp
puts n.length

STEP: 2 足したり引いたり

n, a, b = gets.split.map(&:to_i)
if n + a + b == 0
    puts "YES"
elsif n + a - b == 0
    puts "YES"
elsif n - a + b == 0
    puts "YES"
elsif n - a - b == 0
    puts "YES"
else
    puts "NO"
end

nが0になる4パターンの足し引きを検証する愚直な条件分岐が模範回答でした。

STEP: 3 同値判定

n = gets.to_i
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
ans = 0
n.times do |i|
    if a[i] == b[i]
        ans += 1
    end
end
puts ans

STEP: 4 終了判定

n = gets.to_i
a = gets.split.map(&:to_i)
ans = 0
a.each do |i|
    if i.odd?
        break
    end
    ans += i
end
puts ans

STEP: 5 終了判定 2

n, k = gets.split.map(&:to_i)
ans = 0
while n < k
    n *= 2
    ans += 1
end
puts ans

STEP: 6 池の周回

n, k, t = gets.split.map(&:to_i)
puts k * t % n == 0 ? "YES" : "NO"

STEP: 7 崖に落ちるか判定

n, k, t = gets.split.map(&:to_i)
puts n * k <= t ? "YES" : "NO"

条件よりn, k, tは整数なので問題文の0.1については無視して<=で大丈夫です。

STEP: 8 タイルの敷き詰め

h, w = gets.split.map(&:to_i)
puts h.nonzero? && w.nonzero? && h.even? && w.even? ? "YES" : "NO"

even?は0でもtrueを返すので、nonzero?で0をfalseにする必要があります。

FINAL問題 積の最小化

a, b = gets.split.map(&:to_i)
# AからBの間に 0 が含まれている場合Aが負の値、Bが正の値なので A * B が答え
if a <= 0 && b >= 0
    puts a * b
# A、B両方とも正の整数の場合 A * A が答え
elsif a > 0 
    puts a * a
# A、B両方とも負の整数の場合 B * B が答え
else
    puts b * b
end
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?