不正确的字符串计数使用c ++的Euler 17项目 [英] Incorrect string count Project Euler 17 using c++

查看:132
本文介绍了不正确的字符串计数使用c ++的Euler 17项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个代码来执行Project Euler 17问题,其中写道:
如果数字1到5以字:1,2,3,4,5,则有3 + 3 + 5 + 4 + 4 =总共使用了19个字母。如果所有的数字从1到1000(一千)包括在内,用字写出,将使用多少字母?//
注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。在写出数字时使用和符合英国的使用。

I have written a code to execute Project Euler 17 problem which states: If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?// NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

但是,我的代码似乎没有输出正确的答案。有人可以帮助我理解我在做错误,以计算我的代码中的字符?

我的代码(输出)发布在:
http://codepad.org/TEV4utVx

However, my code does not seem to output the right answer. Can someone help me understand the mistake I am doing to count the characters in my code?
My code (with output) is posted on: http://codepad.org/TEV4utVx

推荐答案

if(i>20 && i<=100) 
{
  count  += it1->second.length();
  count  += it2->second.length();
}

总是添加两个单词的计数。考虑i = 30的情况,这应该是三十而不是三十零,我想你会看到的问题。如果在计算之前创建实际的字(可能打印),可能有助于调试。

Are always adding the counts for two words. Consider the case when i = 30, which should be "thirty" not "thirty zero" and I think you'll see the problem. It might be helpful for debugging if you create the actual word before counting it (possibly printing it).

这篇关于不正确的字符串计数使用c ++的Euler 17项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆