Problem 17 Number letter counts (统计字母)

来源:互联网 发布:阿里云飞天六部 编辑:程序博客网 时间:2024/06/05 11:30

Number letter counts

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.


Answer:
21124Completed on Thu, 27 Oct 2016, 05:16

题解:从1算到100,再从100 算到1000.

代码:

#include <bits/stdc++.h>using namespace std;int main(){  //1-9  int s1[] = { 0, 3, 3, 5, 4, 4, 3, 5, 5, 4 }; //10-19  int s2[] = { 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 };  //20 30 40 50 60 70 80 90  int s3[] = { 0, 0, 6, 6, 5, 5, 5, 7, 6, 6 };  int hundred = 7;  int And = 3;  int thousand = 8;  int i;  int sum = 0;    //1-9  for (i = 1; i < 10; i++)  {    sum += s1[i];  }    //10-19  for (i = 0; i < 10; i++){    sum += s2[i];  }   //20-99  for (i = 20; i < 100; i++){    sum += s3[i/10] + s1[i%10];  }   //100-999  for (i = 1; i < 10; i++)  {    int j;   //计算hundred     sum += s1[i] + hundred;    // i*100 + j    for (j = 1; j < 10; j++){      sum += s1[i] + hundred + And + s1[j];    }    //i*100 + j*10     for (j = 0; j < 10; j++) {      sum += s1[i] + hundred + And + s2[j];    }    //i*100 +j*10 +k    for (j = 20; j < 100; j++) {      sum += s1[i] + hundred + And + s3[j/10] + s1[j%10];    }  }    //1000  sum += 3 + thousand;    cout<<sum<<endl;  return 0;}


1 0
原创粉丝点击