Participating in competitive programming as part of learning Ruby and algorithms. Here, we will output what we have learned during learning.
This time about standard output. I've been using print mainly for some reason so far, I thought it would be better if I could make a distinction, so I will summarize it.
** p method ** -Output that reflects the object type (character string type is output surrounded by ** "" **. Integer type remains as it is) ・ Line breaks for each line
** puts method ** -Converts an object to a character string and outputs it ・ Line breaks for each line
** print method ** -Converts an object to a character string and outputs it ・ ** No line breaks **
Outputs that reflect the type of the object. Since the character string is output in the form surrounded by ** "" **, If the answer is a character string, the answer will be incorrect. There is no problem if you answer with an integer.
p "test"
p 123
output
"test"
123
Converts the object to a character string and outputs it. A line break occurs at the end of the string.
puts "test"
puts 123
output
test
123
Converts the object to a character string and outputs it. Unlike the puts method, there are no line breaks.
print "test"
print 123
output
test123
When outputting two objects in succession as described above, the character strings are output in a connected state.
Since the output format is slightly different, it seems necessary to use it properly depending on the problem.
From "Atcoder Biginners Selection" Question 2 "Product"
There are two positive integers a and b. Output Even if the product of a and b is even, and Odd if it is odd.
Input example
3 4
Answer example
a, b = gets.split(" ").map(&:to_i)
p (a*b).odd? ? "Odd" : "Even" #p method
puts (a*b).odd? ? "Odd" : "Even" #puts method
print (a*b).odd? ? "Odd" : "Even" #print method
output
"Even" #p method incorrect answer
Even #puts method correct answer
Even #print method correct answer
** "" The p method enclosed in ** was incorrect. The answer with the puts method or the print method is the correct answer.
From "Atcoder Biginners Selection" Question 3 "Placing Marbles"
There is a square consisting of three squares. "0" or "1" is written on each square, and marbles are placed on the square with "1" written on it. Please answer how many squares the marbles are placed on.
Input example
101
Answer example
ary = gets.split("").map(&:to_i)
count = 0
ary.each{|i| i==1 ? count+=1 : count+=0}
p count #p method
puts count #puts method
print count #print method
output
2 #p method correct answer
2 #puts method correct answer
2 #print method correct answer
In this question, any pattern of output was correct.
From "Atcoder Biginners Selection" Question 1 "Welcome to AtCoder"
Given the integers a, b, c and the string s. Output the calculation result of a + b + c and the character string s side by side with a half-width space in between.
Input example
1
2 3
test
Answer example
a = gets.to_i
b, c = gets.split(" ").map(&:to_i)
s = gets.chomp
p "#{a+b+c} #{s}" #p method
puts "#{a+b+c} #{s}" #puts method
print "#{a+b+c} #{s}" #print method
output
"6 test" #p method incorrect answer
6 test #puts method correct answer
6 test #print method correct answer
In this problem as well, as in problem example (1), only the p method was incorrect. ** For problems that output a character string and answer, it seems better to answer with the puts method or print method. ** **
That's all about how to use the standard output that is often used.
If you have any mistakes, I would be grateful if you could point them out.
Recommended Posts