查看完整版本: 求自己的幸运数字.

kran 2008-8-28 10:07

求自己的幸运数字.

[quote]原帖:[url]http://bbs.blueidea.com/thread-2881905-1-1.html[/url]

题目:首先把你的生日列出来 比如 1987 12 25
然后一位位的相加 1+9+8+7+1+2+2+5=35
把得出的数字再拆分 再加 3+5=8

最后向屏幕输出:8[/quote]

用ruby写出的最简方法是什么?:lol

glight 2008-8-29 13:20

只能想到基本的方法:[code]a = '19871225'
while a.length > 1 do
  sum = 0
  a.each_byte {|char| sum += (char - 48)}
  a = sum.to_s
end

puts "Your lucky number is #{a}"[/code]

hjlarry 2008-11-2 17:31

LS正解

kelvin 2008-11-4 18:52

终于..................

LZ  不错!!

diyuxinlang 2008-11-5 11:38

birthday = '19820614'
class Array
  def sum
    num = 0
    self.each do |f|
        num += f.to_i
      end
    num
  end
end
p birthday.split('').sum.to_s.split('').sum

diyuxinlang 2008-11-8 09:32

又一解法

birthday, count, result = '19820614', 0, 0
birthday.split('').each {|v| count += v.to_i }
count.to_s.split('').each {|v| result += v.to_i }
p result
页: [1]
查看完整版本: 求自己的幸运数字.