Skip to content

Instantly share code, notes, and snippets.

@sfengyuan
Created March 8, 2019 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfengyuan/1b12a8210695c370bc6c2f8d6e0aa98c to your computer and use it in GitHub Desktop.
Save sfengyuan/1b12a8210695c370bc6c2f8d6e0aa98c to your computer and use it in GitHub Desktop.
# 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