欧拉计划问题 17 - 出了什么问题?
我今天决定尝试项目欧拉问题 17,我很快用 C++ 编写了一个相当快的代码来解决它。然而,由于某种原因,结果是错误的。 问题是:
如果将数字1到5写成单词:一、二、三、四、五,那么总共使用了3 + 3 + 5 + 4 + 4 = 19个字母。
如果从 1 到 1000(一千)的所有数字都用文字写出来,需要使用多少个字母?
注意:不要计算空格或连字符。例如,342(三百四十二)包含 23 个字母,115(一百一十五)包含 20 个字母。写数字时使用“and”符合英国习惯。
我真的不知道为什么,因为我已经彻底检查了程序的每个部分,但找不到任何问题。我发现的唯一不好的事情是当检查 1000 时,我的 while 循环没有正确检查。我通过将 while 循环的限制降低到 <1000 而不是 <1001 来解决这个问题,然后手动将 11(onethousand = 11) 添加到总和中。然而,它不起作用。如果您能告诉我出了什么问题,我将不胜感激。我确信我的代码很糟糕,但几分钟内就完成了。所以这里是:
int getDigit (int x, int y)
{
return (x / (int)pow(10.0, y)) % 10;
}
int main()
{
string dictionary[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string dictionary2[18] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
string dictionary3[10] = { "onehundred", "twohundred", "threehundred", "fourhundred", "fivehundred", "sixhundred", "sevenhundred", "eighthundred", "ninehundred", "onethousand" };
int i = 1;
int last;
int first;
int middle;
_int64 sumofletters = 0;
while (i < 10) //OK
{
sumofletters += dictionary[i].length();
i++;
}
cout << sumofletters << endl;
while (i < 20) //OK
{
last = i % 10;
sumofletters += dictionary2[last].length();
i++;
}
while (i < 100) //OK
{
first = (i / 10) + 8;
last = i % 10;
if (last != 0)
{
sumofletters += dictionary2[first].length() + dictionary[last].length();
}
else
sumofletters += dictionary2[first].length();
i++;
}
cout << sumofletters << endl;
while (i < 1000) //OK
{
last = i % 10;
first = (i / 100) - 1;
middle = (getDigit(i, 1)) + 8;
if (middle != 0 && last != 0) //OK
{
if (middle == 1)
sumofletters += dictionary3[first].length() + dictionary2[last].length() + 3;
else
sumofletters += dictionary3[first].length() + dictionary2[middle].length() + dictionary[last].length() + 3;
}
else if (last == 0 && middle != 0) //OK
{
if (middle == 1)
sumofletters += dictionary3[first].length() + 6;
else
sumofletters += dictionary3[first].length() + dictionary2[middle].length() + 3;
}
else if (middle == 0 && last != 0) //OK
sumofletters += dictionary3[first].length() + dictionary[last].length() + 3;
else
sumofletters += dictionary3[first].length();
i++;
}
sumofletters += 11;
cout << sumofletters << endl;
return 0;
}
I decided to try project euler problem 17 today, and I quickly wrote a pretty fast code in C++ to solve it. However, for some reason, the result is wrong.
The question is:
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.
I seriously don't know why, as I have checked every part of my program thoroughly and I can't find anything wrong. The only thing bad I could find is when checking for 1000, my while loop doesn't check correctly. I fixed that by lowering the limit of my while loop to <1000 instead of <1001 and just added 11(onethousand = 11) manually to the sum. And yet, it doesn't work. I would really appreciate it if you could tell me what's wrong. I'm sure my code is pretty bad, but it's something done in a few minutes. So here it is:
int getDigit (int x, int y)
{
return (x / (int)pow(10.0, y)) % 10;
}
int main()
{
string dictionary[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string dictionary2[18] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
string dictionary3[10] = { "onehundred", "twohundred", "threehundred", "fourhundred", "fivehundred", "sixhundred", "sevenhundred", "eighthundred", "ninehundred", "onethousand" };
int i = 1;
int last;
int first;
int middle;
_int64 sumofletters = 0;
while (i < 10) //OK
{
sumofletters += dictionary[i].length();
i++;
}
cout << sumofletters << endl;
while (i < 20) //OK
{
last = i % 10;
sumofletters += dictionary2[last].length();
i++;
}
while (i < 100) //OK
{
first = (i / 10) + 8;
last = i % 10;
if (last != 0)
{
sumofletters += dictionary2[first].length() + dictionary[last].length();
}
else
sumofletters += dictionary2[first].length();
i++;
}
cout << sumofletters << endl;
while (i < 1000) //OK
{
last = i % 10;
first = (i / 100) - 1;
middle = (getDigit(i, 1)) + 8;
if (middle != 0 && last != 0) //OK
{
if (middle == 1)
sumofletters += dictionary3[first].length() + dictionary2[last].length() + 3;
else
sumofletters += dictionary3[first].length() + dictionary2[middle].length() + dictionary[last].length() + 3;
}
else if (last == 0 && middle != 0) //OK
{
if (middle == 1)
sumofletters += dictionary3[first].length() + 6;
else
sumofletters += dictionary3[first].length() + dictionary2[middle].length() + 3;
}
else if (middle == 0 && last != 0) //OK
sumofletters += dictionary3[first].length() + dictionary[last].length() + 3;
else
sumofletters += dictionary3[first].length();
i++;
}
sumofletters += 11;
cout << sumofletters << endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要为你做你的工作:
将其分解为更小的功能。
然后您可以独立测试每个功能。
如果需要的话,然后编写一些单元测试,或者只是使用调试器并单步执行,并在一张纸上执行它,看看您和您的代码在哪里存在分歧。
Rather than doing your work for you:
Split it up in to smaller functions.
Then you can test each function independently.
Write some unit tests then if you need to, or just use a debugger and step through and do it also on a bit of paper and see where you and your code diverge.
四十拼写错误,应该是四十。
检查您的比较 middle == 0 / middle != 0 / middle = 0。回头看看您计算 middle 的位置。那是错误的。
解决这两个问题就可以得到正确的答案。
fourty is mispelt and should be forty.
Check your comparisons middle == 0 / middle != 0 / middle = 0. Go look back at where you calculate middle. That's wrong.
Fixing both of those gets the right answer.
问题似乎出在这一行:
您将 8 添加到这个数字 - 大概是作为
dictionary2
的偏移量 - 但在下面的 if 语句中,您会遇到需要将其设置为 0 的情况。除非 getDigit 返回 -8,否则这些永远无法满足。不要在那里添加偏移量,而是在需要时添加它 - 或者更好的是,不要将这些内容存储在同一个字典中。
更好的是完全不同的结构:编写一个函数来生成数字的字符串,然后获取该字符串的长度进行计数。这也将使调试此类问题变得更加容易,因为您可以看到正在获取的实际字符串长度。
The problem appears to be with this line:
You add 8 to this number - presumably to act as an offset into your
dictionary2
- but in the following if statements, you have cases where it needs to be 0. Those can never be satisfied unless getDigit would return -8.Instead of adding your offset there, add it when you need it - or better still, don't store those things in the same dictionary.
Even better would be a completely different structure: write a function which generates the string for a number, and then take the length of that string for your counting. This will also make it much easier to debug problems like this, because you can see the actual string you're taking the length of.