Project Euler Problem 17 - What's wrong?

时光毁灭记忆、已成空白 提交于 2019-12-11 07:36:41

问题


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;
}

回答1:


The problem appears to be with this line:

middle = (getDigit(i, 1)) + 8; 

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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4245811/project-euler-problem-17-whats-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!