首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Project Euler (#17)

Project Euler (#17)
EN

Stack Overflow用户
提问于 2011-10-30 18:51:03
回答 2查看 939关注 0票数 3

关于项目euler的问题17说明:

如果数字1到5写成单词:1,2,3,4,5,那么总共使用了3+3+5+4+4= 19个字母。如果从1到1000 (包括1000)的所有数字都用单词写出,会使用多少个字母?注意:不要计算空格或连字符。例如,342 (342)包含23个字母,第115( 115 )包含20个字母。在写出数字时使用"and“符合英国的用法。

我已经检查了我的代码很多次,但找不到为什么它不能正确地解决它,任何帮助都将非常感谢,谢谢

代码语言:javascript
复制
`#include <iostream>
#include <sstream>

unsigned int value = 11;//one thousand = 11

short small(short x);
void two(short third);

int main()
{
    for(short count = 0;count<10;count++)
    {
        two(count);
    }
   std::cout<<value;
    std::cin.get();
    return 0;
}

void two(short third)
{

    std::string temp;
    if(third>0)
    {
         third = (small(third) + 10);//10 = and(3) + hundred(7)
    }
    for(short i = 0;i<20;i++)//0-20
    {   
        value += (small(i) + third);      
    }
    for(short i = 20;i<60;i++)//20-40 + 80-100
    {
         std::stringstream ss;
         ss<<i;
         temp = ss.str();
         value += ((small(temp[1]-'0') + 6) + third);
    }
    for(short i = 40;i<70;i++)//40-70
    {
         std::stringstream ss;
         ss<<i;
         temp = ss.str();
         value += ((small(temp[1]-'0') + 5) + third);
    }
    for(short i = 70;i<80;i++)//70-80
    {
         std::stringstream ss;
         ss<<i;
         temp = ss.str();
         value += ((small(temp[1]-'0') + 7) + third);
    }      
}

short small(short x)
{
              switch(x)
             {
                case 0:
                     return 0;
                case 1:
                     return 3;
                case 2:  
                     return 3;
                case 3:
                     return 5;
                case 4:
                     return 4;
                case 5:
                     return 4;
                case 6:
                     return 3;
                case 7:
                     return 5;
                case 8:
                     return 5;
                case 9:
                     return 4;
                case 10:
                     return 3;
                case 11:
                     return 6;
                case 12:
                     return 6;
                case 13:
                     return 8;
                case 14:
                     return 8;
                case 15:
                     return 7;
                case 16:
                     return 7;
                case 17:
                     return 9;
                case 18:
                     return 8;
                case 19:
                     return 8;                   
          }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-30 20:19:11

首先,您需要考虑John Mashall的答案。使用字符串的解决方案不是最优的,您可以考虑使用模数10来提取第二个数字。

你没有把100,200,300,400加起来...你加上101,102,...正确,但不是整数百。在添加首字母和百(10个字符)的代码中,还应该添加百(7) (和一、二、三、...)的长度。设置为值:

代码语言:javascript
复制
if(third>0)
{
     third = (small(third) + 10);//10 = and(3) + hundred(7)
     value += third - 3; // no need for the "and"
}

如果我应用John Marshalls fix (改为使用模数)并应用上述方法,我会得到正确的结果(将鼠标悬停在下面的框上查看结果):

21124

票数 1
EN

Stack Overflow用户

发布于 2011-10-30 19:28:31

您的第一个错误是

代码语言:javascript
复制
ss.str() = "";

并不是你想的那样。您应该在每个循环中打印出temp,以检查是否向value添加了您认为要添加的内容。

然后,您应该简化问题:检查您的程序是否为1到10、1到20、1到100、1到110等生成了正确的数量并将其添加到value中。您会发现,如果对程序进行了组织,以便您可以计算任何1到n的和--例如,对于命令行上给定的n,那么调试程序会更容易。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7944504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档