项目欧拉问题17 - 怎么了? [英] Project Euler Problem 17 - What's wrong?

查看:263
本文介绍了项目欧拉问题17 - 怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定今天尝试项目欧拉问题17,我很快就写了pretty快速code在C ++来解决它。然而,由于某种原因,结果是错误的。
现在的问题是:

如果1至5在字写出的数字:一个,两个,三个,四个,五个,则有3 + 3 + 5 + 4 + 4 =总共使用19的信

如果都从1到1000(千),其中包括数字在话写出来,多少个字母会使用吗?

请注意:不要指望空格或连字符。例如,342(342),包含23个字母和115(115),包含20个字母。使用和写出数字时符合英国用法。

我严重不知道为什么,因为我已经彻底检查我的程序的每一个部分,我不能发现任何错误。坏我唯一能找到的是1000检查时,我while循环没有正确地检查。我固定,通过降低我的while循环的限制至< 1000的代替&所述; 1001和刚添加11(onethousand = 11)手动的总和。然而,这是行不通的。我真的AP preciate它,如果你能告诉我什么是错的。我敢肯定,我的code是pretty坏的,但它的东西在几分钟内完成。所以在这里,它是:

  INT getDigit(INT X,int y)对
{
 回报(X /(INT)POW(10.0,y))为10%;
}诠释的main()
{
 串词典[10] = {零,一,二,三,四有,五,六个一,七,八,九};
 串dictionary2 [18] = {十,十一,十二条,十三,十四,十五,十六条,十七,十八,古诗十九首,廿 三十,四十,五十,六十条,七十,八十,90};
 串dictionary3 [10] = {onehundred,twohundred,threehundred,fourhundred,fivehundred,sixhundred,sevenhundred,eighthundred,ninehundred,onethousand}; INT I = 1;
 INT最后;
 诠释第一;
 INT中间; _int64 sumofletters = 0; 而(ⅰ小于10)//确定
 {
  sumofletters + =词典[I]。长度();  我++;
 } COUT<< sumofletters<< ENDL; 而(I< 20)// OK
 {
  LAST = 1%10;  sumofletters + = dictionary2 [尾页]。长度();  我++;
 } 而(I< 100)// OK
 {
  第一=(I / 10)+ 8;
  LAST = 1%10;  如果(即止!= 0)
  {
   sumofletters + = dictionary2 [首页]。长度()+词典[最后]。长度();
  }  其他
   sumofletters + = dictionary2 [首页]。长度();  我++;
 } COUT<< sumofletters<< ENDL; 而(I< 1000)// OK
 {
  LAST = 1%10;
  第一=(I / 100) - 1;
  中间=(getDigit(I,1))+ 8;  如果(中= 0&安培;!&安培;最后!= 0)// OK
  {
   如果(中间== 1)
    sumofletters + = dictionary3 [首页]。长度()+ dictionary2 [最后]。长度()+ 3;
   其他
    sumofletters + = dictionary3 [首页]。长度()+ dictionary2 [中]。长度()+词典[最后]。长度()+ 3;
  }  否则,如果(最后== 0安培;&安培;中!= 0)// OK
  {
   如果(中间== 1)
    sumofletters + = dictionary3 [首页]。长度()+ 6;
   其他
    sumofletters + = dictionary3 [首页]。长度()+ dictionary2 [中]。长度()+ 3;
  }  否则,如果(中== 0安培;&安培;!最近= 0)// OK
   sumofletters + = dictionary3 [首页]。长度()+词典[最后]。长度()+ 3;  其他
   sumofletters + = dictionary3 [首页]。长度();  我++;
 } sumofletters + = 11; COUT<< sumofletters<< ENDL; 返回0;
}


解决方案

这个问题似乎与这一行:

 中部=(getDigit(I,1))+ 8;

您加入8这个数字 - presumably作为偏移到 dictionary2 - 但在下面的if语句,你有案件需要的地方为0。那些永远无法满足,除非getDigit将返回-8。

而不是增加您的偏移出现,添加它当你需要它的 - 或者更好的是,不要那些东西存储在同一字典

更妙的是一个完全不同的结构:写一个生成一个数字字符串的函数,然后采取字符串的长度为您计算。这也将使它更以这样的调试问题更​​容易,因为你可以看到你正在采取的实际长度的字符串。

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

解决方案

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.

这篇关于项目欧拉问题17 - 怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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