2020/05/17
When I was doing competitive programming with Atcoder Keep a memorandum of the mistakes you made.
Atcoder168_B
OK if the following is displayed in the output
$ ruby Bmain.rb
4 #input
aaaaa #input
aaaa... #output
I wrote the following code.
K = gets.to_i
S = gets.chomp
if S.length <= K then
p S
elsif S.length > K then
p S[0..(K-1)]+"..."
end
output An error occurs because "" is displayed in the output.
$ ruby Bmain.rb
4 #input
aaaaa #input
"aaaa..." #output
Change p to puts
K = gets.to_i
S = gets.chomp
if S.length <= K then
puts S
elsif S.length > K then
puts S[0..(K-1)]+"..."
end
The following was displayed without "".
$ ruby Bmain.rb
4 #input
aaaaa #input
aaaa... #output
Since p is for debugging purposes, "" is added for easy understanding.
Recommended Posts