返回顶部

收藏

Ruby 九九乘法表

更多
(1..9).map{|a| (1..a).map{|b| "#{b}*#{a}=#{a*b}" }} .flatten

=> ["1*1=1", "1*2=2", "2*2=4", "1*3=3", "2*3=6", "3*3=9", "1*4=4", "2*4=8", "3*4=12", "4*4=16", "1*5=5", "2*5=10", "3*5=15", "4*5=20", "5*5=25", "1*6=6", "2*6=12", "3*6=18", "4*6=24", "5*6=30", "6*6=36", "1*7=7", "2*7=14", "3*7=21", "4*7=28", "5*7=35", "6*7=42", "7*7=49", "1*8=8", "2*8=16", "3*8=24", "4*8=32", "5*8=40", "6*8=48", "7*8=56", "8*8=64", "1*9=9", "2*9=18", "3*9=27", "4*9=36", "5*9=45", "6*9=54", "7*9=63", "8*9=72", "9*9=81"]

(1..9).map{|a| (1..a).map{|b| "#{b}*#{a}=#{a*b}" }} .join ' '

=> "1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81"

标签:ruby

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2014-01-08 14:38:53 Ruby on Rails 中使用 Markdown 语法,及代码高亮
  2. 博主 发表 2014-01-13 10:23:02 用 Nginx 和 Unicorn 部署 Ruby on Rails 应用
  3. 博主 发表 2018-06-20 00:00:00 Support of Ruby 2.2 has ended
  4. 博主 发表 2018-06-19 00:26:00 ruby/rails - 根据浏览器的语言,来自动识别
  5. Alex Wood 发表 2014-10-15 13:06:29 Deploying Ruby on Rails Applications to AWS OpsWor
  6. Alex Wood 发表 2014-10-22 12:04:19 Caching the Rails Asset Pipeline with Amazon Cloud
  7. Trevor Rowe 发表 2014-11-18 19:04:17 Using Resources
  8. Trevor Rowe 发表 2014-11-20 19:04:22 Waiters
  9. Trevor Rowe 发表 2014-11-20 19:04:22 Waiters
  10. Trevor Rowe 发表 2014-11-26 19:04:20 Client Response Stubs
  11. Alex Wood 发表 2014-12-02 17:32:13 AWS re:Invent 2014 Ruby Recap
  12. Alex Wood 发表 2014-12-10 17:20:18 Using Amazon RDS with Ruby on Rails and AWS OpsWor

发表评论