我在接受采访时得到了这个问题并得到了几乎所有答案,但最后一部分却陷入困境.例如,如果我想获取5的乘法表,我想让输出格式如下: 1, 2, 3, 4, 52, 4, 6, 8, 103, 6, 9, 12, 154, 8, 12, 16, 205, 10
1, 2, 3, 4, 5 2, 4, 6, 8, 10 3, 6, 9, 12, 15 4, 8, 12, 16, 20 5, 10, 15, 20, 25
我对此的回答是:
def make_table(n) s = "" 1.upto(n).each do |i| 1.upto(n).each do |j| s += (i*j).to_s end s += "\n" end p s end
但是make_table(5)的输出是:
"12345\n246810\n3691215\n48121620\n510152025\n"
我尝试过使用数组的变体,但我得到了类似的输出.
我错过了什么或者我应该如何看待问题的最后部分?
您可以使用map和join在一行中获取String:n = 5 puts (1..n).map { |x| (1..n).map { |y| x * y }.join(', ') }.join("\n")
它遍历行(x = 1,x = 2,…).对于每一行,它迭代单元格(y = 1,y = 2,…)并计算x * y.它连接一行中的每个单元格,并使用换行符连接表格中的每一行:
1, 2, 3, 4, 5 2, 4, 6, 8, 10 3, 6, 9, 12, 15 4, 8, 12, 16, 20 5, 10, 15, 20, 25
如果要保持逗号对齐,可以使用rjust:
puts (1..n).map { |x| (1..n).map { |y| (x * y).to_s.rjust(3) }.join(',') }.join("\n")
它输出:
1, 2, 3, 4, 5 2, 4, 6, 8, 10 3, 6, 9, 12, 15 4, 8, 12, 16, 20 5, 10, 15, 20, 25
在对齐逗号之前,您甚至可以想象并计算n ** 2的宽度:
n = 11 width = Math.log10(n**2).ceil + 1 puts (1..n).map { |x| (1..n).map { |y| (x * y).to_s.rjust(width) }.join(',') }.join("\n")
它输出:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121