C++字符数(Project Euler 17错误答案)
C++ character count (Project Euler 17 wrong answer)
当我在Project Euler中编写代码来解决问题17时,得到了一个错误的答案。我正在输出每个字符串以使计数更加清晰
链接发布在这里: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;
}
您输出的是"一百"而不是"一百"。
-
您可以简化大多数包含事例列举的测试:
if ((i != 30)&&(i != 40)&&(i != 50)&&(i != 60)&&(i != 70)&&(i != 80)&&(i!= 90)&&(i!= 100))
当(i>20&i<=100)时,您可以检查i是否是10的倍数(例如使用可变单位)
if((i != 200)&&(i != 300)&&(i != 400)&&(i != 500)&&(i != 600)&&(i != 700)&&(i != 800)&&(i != 900))
当(i>100&&i<1000)时,您可以检查i是否是100 的倍数
if ((ten != 30)&&(ten != 40)&&(ten != 50)&&(ten != 60)&&(ten != 70)&&(ten != 80)&&(ten!= 90)&&(ten!= 100))
当(ten>20&&ten<=99)时,您可以检查10是否是10的倍数。
-
一般来说,代码越短,就越容易发现错误。这包括删除无用的测试(由于变量的边界,上面的一些条件是无用的),删除重复的代码,等等。
相关文章:
- 递归求和任务的错误答案
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 寻找最小楼梯成本的动态规划问题的错误答案
- 为什么可变大小的数组会导致Codechef出现错误答案
- 如何修复此代码对几个测试用例的错误答案?
- 使用幂函数的计算给出了大数字的错误答案
- 快速排序算法,一些特定输入序列的错误答案和分段错误
- 二分法程序的错误答案(C++)
- 无法理解绘图程序中的错误答案
- C++ - 整数除以整数返回错误答案
- 布尔函数的错误答案
- SPOJ FASTFLOW上的错误答案
- 代码的错误答案是在Java Camel案件和C 下划线标识符之间转换的错误答案
- 200万以下所有素数的总和,线程给出错误答案
- 0-1整数背包返回错误答案(动态编程)
- UVa 在线判断 - 3n + 1 - 错误答案
- 提升C++原始算法错误答案
- 二项式系数函数C++错误答案 n>13
- 需要帮助以避免 SPOJ 上的"错误答案"