(Problem 17)Number letter counts

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.

 

题目大意:

如果用英文写出数字1到5: one, two, three, four, five, 那么一共需要3 + 3 + 5 + 4 + 4 = 19个字母。

如果数字1到1000(包含1000)用英文写出,那么一共需要多少个字母?

注意: 空格和连字符不算在内。例如,342 (three hundred and forty-two)包含23个字母; 115 (one hundred and fifteen)包含20个字母。"and" 的使用与英国标准一致。

// (Problem 16)Number letter counts

// Completed on Sun, 17 Nov 2013, 16:30

// Language: C

//

// 版权所有(C)acutus   (mail: [email protected]) 

// 博客地址:http://www.cnblogs.com/acutus/

#include <stdio.h> 

#include <stdbool.h>



int a[101] = {0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8};



void init(void)  //初始化数组

{

    a[20] = 6;

    a[30] = 6;

    a[40] = 5;

    a[50] = 5;

    a[60] = 5;

    a[70] = 7;

    a[80] = 6;

    a[90] = 6;

    a[100] = 7;

}



int within100(void)  //计算1~99所含字母的和

{

    int i, sum, t;

    t = sum = 0;

    for(i = 1; i <= 9; i++) t += a[i];

    for(i = 1; i <= 19; i++) sum += a[i];

    for(i = 2; i <= 9; i++) {

        sum += a[i*10] * 10;

        sum += t;

    }

    return sum;

}



void solve(void)

{

    int i;

    int sum, t;

    sum = t = within100();

    for(i = 1; i < 10; i++) {

        sum += (a[i] + 10) * 99 + (a[i] + 7) + t;

    }

    sum += 11;



    printf("%d\n",sum);

}



int main(void)

{

    init();

    solve();

    return 0;

}
Answer:
21124

你可能感兴趣的:(number)