我决定今天尝试项目欧拉问题 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;
}