AtCoder Beginner Contest A - ∴ (Therefore) Difficulty: 2
This theme, case expression Ruby In the main battle, it is solved by the ʻif` formula, but in * editorial *
As an aside, in Ruby, the case statement corresponds to this, and it is more sophisticated.
Since it is written, I would like to write it in case
. ~~ I have never written a case
~~
First of all, ʻif`
ruby.rb
n = gets.to_i
n %= 10
if n == 3
puts "bon"
elsif n == 0 || n == 1 || n == 6 || n == 8
puts "pon"
else
puts "hon"
end
last.rb
n %= 10
I get the 1's place by the remainder divided by 10, but I can also make it a string as gets.chomp
and make itn [-1]
.
case.rb
n = gets.chomp
case n[-1]
when "3" then
puts "bon"
when "0", "1", "6", "8" then
puts "pon"
else
puts "hon"
end
,
. is.
Pythonpython.py
n = int(input()) % 10
if n == 3:
print("bon")
elif n in {0, 1, 6, 8}:
print("pon")
else:
print("hon")
Python doesn't seem to have a case
, so I tried to make it look similar.
Python's ʻin is similar to SQL's ʻin
.
Referenced site
Recommended Posts