我决定今天试试euler问题17,并且我很快用C++编写了一个非常快速的代码来解决它。但是,由于某种原因,结果是错误的。 现在的问题是:欧拉项目问题17 - 有什么问题?
如果数字1到5用字写出:1,2,3,4,5,那么共有3 + 3 + 5 + 4 + 4 = 19个字母。
如果所有数字从1到1000(一千)包括在内,都用文字写出来,会用多少个字母?
注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。在编写数字时使用“和”符合英国的用法。
我真的不知道为什么,因为我已经彻底检查了我的程序的每个部分,我找不到任何错误。我能找到的唯一不好的事情是检查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;
}
对于那些不熟悉Project Euler问题17的人,你可以解释它是什么或至少给出一个链接。如果不知道程序应该做什么,你不能指望人们告诉你问题在哪里。 – ereOn 2010-11-22 13:25:18
哦,是的..忘了这一点。编辑:P – Lockhead 2010-11-22 13:27:00
我建议你用计算机的方式解决你的计算难题:以容易验证的形式得到输出结果 - 打印出每个数字的文本描述,并计算字母数量,然后得到仔细观察,并做一个完整的检查,特别是在比较特殊的情况下。 – 2010-11-22 13:37:36