multiplication 乘法口诀

multiplication 乘法口诀

#filename:
# multiplication.rb
for x in 1..9
 for y in 1..x
  if x==y
   print x,"*",y,"=",x*y,"\n"
  else
  print x,"*",y,"=",x*y," "
  end
 end
end
不错,就是代码很没有ruby味~
参考这个
http://www.javaeye.com/topic/24642
#filename:
# multiplication.rb
def multiplication(x,y)
 s1=x,"*",y,"=",x*y,"\n"
 s2=x,"*",y,"=",x*y," "
 x==y ?s1:s2
end
(1..9).each{|x|
 (1..x).each{|y| print multiplication(x,y)}
}
这个问题非常有趣,我来一个我写的,看看是不是ruby味:

[Copy to clipboard] [ - ]
生成数组再join是不是会慢一些?
五花八门,各显其能!

鼓励大家多来给出自己的方法!
我也有此疑问,但效率好坏不能考经验跟知觉来猜,profile it!

[Copy to clipboard] [ - ]
小程序引出大思考,向各位管理员学习。