【问题标题】:C++ character count (Project Euler 17 wrong answer) [closed]C++ 字符计数(Project Euler 17 错误答案)[关闭]
【发布时间】:2012-01-11 15:28:18
【问题描述】:

当我编写代码来解决Prob 17 in Project Euler 时,我得到了一个错误的答案。 我正在输出每个字符串以使计数更清晰。

链接贴在这里:http://codepad.org/wE54t7Qi

谁能帮我确定问题出在哪里?

map<long int, string> CharNumberValue;

int main()
{

CharNumberValue[0] ="zero";
CharNumberValue[1] ="one";
CharNumberValue[2] ="two";
CharNumberValue[3] ="three";
CharNumberValue[4] ="four";
CharNumberValue[5] ="five";
CharNumberValue[6] ="six";
CharNumberValue[7] ="seven";
CharNumberValue[8] ="eight";
CharNumberValue[9] ="nine";
CharNumberValue[10] ="ten";
CharNumberValue[11] ="eleven";
CharNumberValue[12] ="twelve";
CharNumberValue[13] ="thirteen";
CharNumberValue[14] ="fourteen";
CharNumberValue[15] ="fifteen";
CharNumberValue[16] ="sixteen";
CharNumberValue[17] ="seventeen";
CharNumberValue[18] ="eighteen";
CharNumberValue[19] ="nineteen";
CharNumberValue[20] ="twenty";
CharNumberValue[30] ="thirty";
CharNumberValue[40] ="forty";
CharNumberValue[50] ="fifty";
CharNumberValue[60] ="sixty";
CharNumberValue[70] ="seventy";
CharNumberValue[80] ="eighty";
CharNumberValue[90] ="ninety";
CharNumberValue[100] ="hundred";//Sameer,remember 100 is one hundred
CharNumberValue[1000] ="thousand";//Sameer,remember 1000 is one thousand

  long int count = 0; 
  string printword ="";

for(int i=1; i< 1000; i++)
{

    if(i<=100)
    {
        int ten = (i/10)*10;
        int unit = i%10;    
        map<long int, string>:: iterator it = CharNumberValue.find(i);
        map<long int, string>:: iterator it1 = CharNumberValue.find(unit);
        map<long int, string>:: iterator it2 = CharNumberValue.find(ten);

        if(i<10)
        {
            count  += it1->second.length();
            printword = it1->second;
            cout<<printword<<endl;
        }
        if(i>=10 && i<=20)
        {
            count  += it->second.length();//These are unique
            printword = it->second;
            cout<<printword<<endl;
        }
        if(i>20 && i<=100) 
        {

            count  += it2->second.length();
            printword = it2->second ;
            if ((i != 30)&&(i != 40)&&(i != 50)&&(i != 60)&&(i != 70)&&(i != 80)&&(i!= 90)&&(i!= 100))
            {
                count  += it1->second.length();
                printword = it2->second + " " + it1->second ;

            }
            cout<<printword<<endl;              
        }           

    }
    if(i>100 && i<1000)
    {
        int hun = i/100;
        int ten=i%100;
        int mten = (ten/10)*10;//modified ten
        int unit = ten%10;

        map<long int, string>:: iterator it = CharNumberValue.find(unit);
        map<long int, string>:: iterator it1 = CharNumberValue.find(mten);          
        map<long int, string>:: iterator it2 = CharNumberValue.find(ten);
        map<long int, string>:: iterator it3 = CharNumberValue.find(hun);           
        int counttemp = CharNumberValue[100].length();
        count  += it3->second.length() + CharNumberValue[100].length();
        printword = it3->second + " " + CharNumberValue[100];

        if((i != 200)&&(i != 300)&&(i != 400)&&(i != 500)&&(i != 600)&&(i != 700)&&(i != 800)&&(i != 900))
        {
            if( ten<=20)
            {
                count  += 3/*for and */+ it2->second.length()   ;//These are unique
                printword = it3->second + " " + CharNumberValue[100]  + " and "  + it2->second ;
                cout<<printword<<endl;
            }

            if(ten>20 && ten<=99) 
            {
                count  += 3/*for and */+ it1->second.length();
                printword = it3->second + " " + CharNumberValue[100]  + " and "  + it1->second;
                if ((ten != 30)&&(ten != 40)&&(ten != 50)&&(ten != 60)&&(ten != 70)&&(ten != 80)&&(ten!= 90)&&(ten!= 100))
                {
                    count  += it->second.length();
                    printword = it3->second + " " + CharNumberValue[100]  + " and "  + it1->second +" "+ it->second ;
                }

                cout<<printword<<endl;
            }

        }
        else
        {
            cout<<printword<<endl;
        }
    }
}
count += 11;//for one thousand
cout<< count;

return 0;
}

【问题讨论】:

  • 如果您将 每个 条目的计数放在其输出旁边,这样您就可以验证总数是否正确,这可能会有所帮助。
  • 我没有时间进行完整的代码审查,但我知道该问题的答案。你想知道这些问题的答案吗:答案是什么?我离正确答案还很远吗?我的答案是太大还是太小?
  • @Josay:我希望我的代码能够生成正确的答案。
  • @SameerShah,一个逻辑故障是你的代码有缺陷。代码审查严格用于工作代码。我们以其他方式改进代码,例如样式/性能而不是正确性。

标签: c++ string


【解决方案1】:

您输出的是“一百”而不是“一百”。

【讨论】:

  • 我知道,但我确实为此添加了 count +=11。这不正确吗?
  • 啊,对不起,我错过了。但是你为什么不循环直到
  • 我实际上正在开发一个函数来计算更大数字的单词。但是为了回答的目的,我刚刚添加了 11。你认为我在那里犯了错误吗?
  • 不,没关系。我用解决方案编辑了我的帖子......这很烦人。
  • 这应该会导致正确的答案。干得好。
【解决方案2】:
  • 您可以简化大多数包含案例枚举的测试:

    if ((i != 30)&&(i != 40)&&(i != 50)&&(i != 60)&&(i != 70)&&(i != 80)&&(i!= 90)&&(i!= 100))
    

    作为 (i>20 && i

    if((i != 200)&&(i != 300)&&(i != 400)&&(i != 500)&&(i != 600)&&(i != 700)&&(i != 800)&&(i != 900))
    

    作为 (i>100 && i

    if ((ten != 30)&&(ten != 40)&&(ten != 50)&&(ten != 60)&&(ten != 70)&&(ten != 80)&&(ten!= 90)&&(ten!= 100))
    

    如 (10>20 && 10

  • 一般来说,代码越短,就越容易发现错误。这包括删除无用的测试(上面的一些条件由于变量的限制而无用)、删除重复的代码等。

【讨论】:

  • 我同意。谢谢乔赛。让我试试这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
相关资源
最近更新 更多