C++程序求debug
题目如下数字1到5用英文写出来:one,two,three,four,five,共用了3+3+5+4+4=19个字符求1到1000用英文写出来总共要用多少个字符?注意:不...
题目如下
数字1到5用英文写出来:one, two, three, four, five,共用了3 + 3 + 5 + 4 + 4 = 19 个字符
求1到1000用英文写出来总共要用多少个字符?
注意:不计连字符和空格。例如: 342 (three hundred and forty-two) 包含23 个字符
115 (one hundred and fifteen) 包含20个字符
我的C++程序如下,但是答案不对,正确答案是21124。
#include<stdio.h>
int table(int num);
int letters_num(int num);
int main()
{
int i = 1;
int sum = 0;
for(i=1; i<=1000; i++)
{
sum += letters_num(i);
}
printf("\nThe result is %d\n",sum);
return 0;
}
int letters_num(const int num)
{
int count =0;
int one=0;
int ten=0;
int oneten=0;
int hundred=0;
int thousand=0;
ten = (int)(num%100)/10;
if(ten != 1) //11--19需要特殊对待!
{
count += table(ten * 10);
one = num%10;
count += table(one);
}
else
{
oneten = num%100;
count += table(oneten);
}
hundred = (int)(num%1000)/100;
if(hundred != 0)
{
count += (table(hundred)+table(100));
if (one !=0 || ten !=0) count += 3; //如果个位与十位上还有数字,则应该加上and
}
thousand = num/1000;
if (thousand != 0) count += (table(thousand)+table(1000));
printf("| %4d %2d |", num, count);if(num%7 == 0)printf("\n");
return count;
}
int table(int num)
{
int letters = 0;
switch(num)
{
case 0:
letters = 0;break;
case 1:
case 2:
case 6:
case 10:
letters = 3;break;
case 4:
case 5:
case 9:
letters = 4;break;
case 3:
case 7:
case 8:
case 40:
case 50:
case 60:
letters = 5;break;
case 11:
case 12:
case 20:
case 30:
case 80:
case 90:
letters = 6;break;
case 15:
case 16:
case 70:
case 100:
letters =7;break;
case 13:
case 14:
case 18:
case 19:
case 1000:
letters = 8;break;
case 17:
letters = 8;break;
}
return letters;
} 展开
数字1到5用英文写出来:one, two, three, four, five,共用了3 + 3 + 5 + 4 + 4 = 19 个字符
求1到1000用英文写出来总共要用多少个字符?
注意:不计连字符和空格。例如: 342 (three hundred and forty-two) 包含23 个字符
115 (one hundred and fifteen) 包含20个字符
我的C++程序如下,但是答案不对,正确答案是21124。
#include<stdio.h>
int table(int num);
int letters_num(int num);
int main()
{
int i = 1;
int sum = 0;
for(i=1; i<=1000; i++)
{
sum += letters_num(i);
}
printf("\nThe result is %d\n",sum);
return 0;
}
int letters_num(const int num)
{
int count =0;
int one=0;
int ten=0;
int oneten=0;
int hundred=0;
int thousand=0;
ten = (int)(num%100)/10;
if(ten != 1) //11--19需要特殊对待!
{
count += table(ten * 10);
one = num%10;
count += table(one);
}
else
{
oneten = num%100;
count += table(oneten);
}
hundred = (int)(num%1000)/100;
if(hundred != 0)
{
count += (table(hundred)+table(100));
if (one !=0 || ten !=0) count += 3; //如果个位与十位上还有数字,则应该加上and
}
thousand = num/1000;
if (thousand != 0) count += (table(thousand)+table(1000));
printf("| %4d %2d |", num, count);if(num%7 == 0)printf("\n");
return count;
}
int table(int num)
{
int letters = 0;
switch(num)
{
case 0:
letters = 0;break;
case 1:
case 2:
case 6:
case 10:
letters = 3;break;
case 4:
case 5:
case 9:
letters = 4;break;
case 3:
case 7:
case 8:
case 40:
case 50:
case 60:
letters = 5;break;
case 11:
case 12:
case 20:
case 30:
case 80:
case 90:
letters = 6;break;
case 15:
case 16:
case 70:
case 100:
letters =7;break;
case 13:
case 14:
case 18:
case 19:
case 1000:
letters = 8;break;
case 17:
letters = 8;break;
}
return letters;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询