Created
March 8, 2019 10:34
-
-
Save sfengyuan/1b12a8210695c370bc6c2f8d6e0aa98c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# https://pintia.cn/problem-sets/17/problems/260 | |
# input example: 19 * | |
# output example: | |
# ***** | |
# *** | |
# * | |
# *** | |
# ***** | |
n, symbol = gets.chomp.split | |
n = n.to_i | |
abort if n < 1 | |
total = row = init = 1 | |
inc = 2 | |
remain = n - init | |
data = [] | |
loop do | |
row += inc | |
data << row | |
total = data.reduce(0, :+) * inc + init | |
break if total > n | |
remain = n - total | |
end | |
data.pop | |
width = data.last || 1 | |
output = [] | |
(output << data.reverse << 1 << data).flatten!.map! do |i| | |
s = '' | |
i.times { |_| s += symbol } | |
s | |
end | |
output.each do |s| | |
printf('%*s', (width - s.length) / 2 + s.length, s) | |
printf "\n" | |
end | |
puts remain |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment