项目Euler#17 Ruby - 出了什么问题?

时间:2015-04-06 12:08:08

标签: ruby letters

我用Ruby制作了一个程序,我不知道为什么它没有给我正确答案

问题: 如果数字1到5用文字写出:一,二,三,四,五,那么总共有3 + 3 + 5 + 4 + 4 = 19个字母。

如果所有1到1000(一千)的数字都用文字写出来,会用多少个字母?

注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。使用"和"写出数字符合英国人的用法。

我的代码:

def number_to_words(n)
    custom = {"0" => "", "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five", "6" => "six", "7" => "seven", "8" => "eight", "9" => "nine", "10" => "ten", "11" => "eleven", "12" => "twelve", "13" => "thirteen", "14" => "fourteen", "15" => "fifteen", "16" => "sixteen",  "17"=> "seventeen", "18" => "eighteen", "19" => "nineteen",}
    tens = {"0" => "", "1" => "ten", "2" => "twenty", "3" => "thirty", "4" => "forty", "5" => "fifty", "6" => "sixty", "7" => "seventy", "8" => "eighty", "9" => "ninety"}
    if n < 20 
        string = custom[n.to_s]     
    elsif n >= 20  &&  n < 100      
        string = tens[n.to_s[0]] + custom[n.to_s[1]]
    elsif n % 1000 == 0
        string = "onethousand"
    elsif n >= 100 && n % 100 == 0 
        string = custom[n.to_s[0]] + "hundred"
    else
        string = custom[n.to_s[0]] + "hundred" + "and" + tens[n.to_s[1]] + custom[n.to_s[2]]
    end
end

def letter_counter(x)
    n = 1
    sum = 0
    while n < x + 1
        sum = sum + number_to_words(n).length
        print number_to_words(n).length, "\n"
        n = n + 1
        print n, "\n"
    end
    results = sum
end

正确答案是

  

21124

3 个答案:

答案 0 :(得分:3)

我真的不懂Ruby,但我确实知道这个Euler问题。在研究了你的代码后,我不得不猜测你的其他陈述是不正确的。

假设您的号码为111,我认为这是您的其他声明。你最终建立一个字符串,上面写着“onehundredandtenone”而不是“onehundredandeleven”

因此,在我看来,111 - 119的范围会产生不正确的字符串,这会破坏你的计数。实际上,对于211 - 219 ...... 311 - 319 ......等等也是如此。

答案 1 :(得分:0)

您可以使用humanize gem:

require 'humanize'

p (1..1000).to_a.map(&:humanize).join.tr(" -", "").size

答案 2 :(得分:0)

我不知道你是否已经知道出了什么问题,但我调试了它。

问题发生在111-120,211-220,311-320附近...... 例如。 number_to_words(111)成为&#34; onehundredandtenone&#34;

所以我又添加了两行来考虑这些实例。

以下是修改:

def number_to_words(n)

custom = {"0" => "", "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five", "6" => "six", "7" => "seven", "8" => "eight", "9" => "nine", "10" => "ten", "11" => "eleven", "12" => "twelve", "13" => "thirteen", "14" => "fourteen", "15" => "fifteen", "16" => "sixteen",  "17"=> "seventeen", "18" => "eighteen", "19" => "nineteen",}
tens = {"0" => "", "1" => "ten", "2" => "twenty", "3" => "thirty", "4" => "forty", "5" => "fifty", "6" => "sixty", "7" => "seventy", "8" => "eighty", "9" => "ninety"}
if n < 20 
  string = custom[n.to_s]     
elsif n >= 20  &&  n < 100      
    string = tens[n.to_s[0]] + custom[n.to_s[1]] 
elsif n == 1000
    string = "onethousand"
elsif n >= 100 && n % 100 == 0 
    string = custom[n.to_s[0]] + "hundred"
#changed this part
elsif n >= 100 && (n % 100 < 20)
    string = custom[n.to_s[0]] + "hundred" + "and" + custom[(n % 100).to_s[(0..1)]] 
else
    string = custom[n.to_s[0]] + "hundred" + "and" + tens[n.to_s[1]] + custom[n.to_s[2]]
    end
    return string
end