I touched Ruby for the first time in class today, so I recorded it as a memorial. The problem of creating the following triangle with only the output of ○. ○ ○○○ ○○○○○ ○○○○○○○
n = gets.to_i #input
for x in 1..n
(n-x).times{print(" ")} #The number of blanks is n-Decrease by 1 from the number of 1
for y in 1..2*(x-1)+1 #The number of ○ is 2*(x-1)+1
print("○")
end
puts
end
First, the blank space on the left side of the circle is output, and then the circle is output. Then, when the output of the circle was finished, I wrote the program in the flow of putting a line break with puts. The number of blanks that can be created on the left side is n-1 in the first row, and is incremented by -1 thereafter. The number of ○ is calculated by 2 (x-1) + 1. When I put the above into the program, I was able to output it safely.
I'm confused because different languages have different specifications, but I want to get used to it quickly.
Recommended Posts