A Little Fool

Fool For Free.

博客园 首页 新随笔 联系 订阅 管理

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.

 

#include <stdio.h>

int numLetters1_19[] =
{
    sizeof("one") - 1,
    sizeof("two") - 1,
    sizeof("three") - 1,
    sizeof("four") - 1,
    sizeof("five") - 1,
    sizeof("six") - 1,
    sizeof("seven") - 1,
    sizeof("eight") - 1,
    sizeof("nine") - 1,
    sizeof("ten") - 1,
    sizeof("eleven") - 1,
    sizeof("twelve") - 1,
    sizeof("thirteen") - 1,
    sizeof("fourteen") - 1,
    sizeof("fifteen") - 1,
    sizeof("sixteen") - 1,
    sizeof("seventeen") - 1,
    sizeof("eighteen") - 1,
    sizeof("nineteen") - 1,
};

int numLetters2Tens[] =
{
    sizeof("twenty") - 1,
    sizeof("thirty") - 1,
    sizeof("forty") - 1,
    sizeof("fifty") - 1,
    sizeof("sixty") - 1,
    sizeof("seventy") - 1,
    sizeof("eighty") - 1,
    sizeof("ninety") - 1,
};

#define GET_NUM_LETTERS_1_19(N) numLetters1_19[N - 1]
#define GET_NUM_LETTERS_2Tens(N) numLetters2Tens[N - 2]

int numLettersHundred = sizeof("hundred") - 1;
int numLettersAnd = sizeof("and") - 1;
int numLettersThound = sizeof("thousand") - 1;

int countLettersLessThan1000(int num)
{
    int numLetters = 0;

    int nThousand = num / 1000;
    if (nThousand > 0)
    {
        numLetters += GET_NUM_LETTERS_1_19(nThousand);
        numLetters += numLettersThound;
    }

    num = num % 1000;

    int nHundred = num / 100;
    bool hasHundred = false;
    if (nHundred > 0)
    {
        numLetters += GET_NUM_LETTERS_1_19(nHundred);
        numLetters += numLettersHundred;
        hasHundred = true;
    }

    num = num % 100;

    if (num > 0)
    {
        if (hasHundred)
        {
            numLetters += numLettersAnd;
        }

        if (num <= 19)
        {
            numLetters += GET_NUM_LETTERS_1_19(num);
        }
        else
        {
            int nTens = num / 10;
            numLetters += GET_NUM_LETTERS_2Tens(nTens);

            num = num % 10;

            if (num > 0)
            {
                numLetters += GET_NUM_LETTERS_1_19(num);
            }
        }
    }

    return numLetters;
}

int main(int argc, char* argv[])
{
    int totalNumLetters = 0;

    for (int i = 1; i <= 1000; ++i)
    {
        totalNumLetters += countLettersLessThan1000(i);
    }

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

    return 0;
}
posted on 2012-09-21 01:07  HenryRead  阅读(475)  评论(0编辑  收藏  举报