欧拉计划Problem 17

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.

void hundred(char t[],int x);
char single[10][10]={"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char tens[10][10]={"","ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
char teen[10][10]={"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
char hun[8]={"hundred"};
int main()
{
 clock_t te,ts;
 ts=clock();
 char t[30]={""};
 int sum = 0;
 for (int i = 999; i >= 1; i--)
 {
  if (i/100 != 0)
  {
   strcat(t,single[i/100]);
   strcat(t,hun);
   if (i% 100 != 0)
   {
    strcat(t,"and");
    hundred(t,i% 100);
   }
  }
  else
  {
   hundred(t,i);
  }
  sum+=strlen(t);
  strcpy(t,"");
 }
 printf("\nanswer: %d",sum+11);
 te=clock();
 printf("\ntime difference: %ds\n",(te-ts)/CLOCKS_PER_SEC);
 return 0;
}

void hundred(char t[],int x)
{
 if (x>0 && x<10)
 {
  strcat(t,single[x]);
 }
 else if(x>10 && x < 20)
 {
  strcat(t,teen[x% 10]);
 }
 else if(x < 100 || x==10)
 {
  strcat(t,tens[x/10]);
  strcat(t,single[x% 10]);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值