惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
J
Java Code Geeks
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
博客园 - 【当耐特】
Y
Y Combinator Blog
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 司徒正美

博客园 - HenryRead

OpenGL 像素在内存中的排列方式 Unreal Engine 4 一些小技巧或提示 介绍Unreal Engine 4中的接口(Interface)使用C++和蓝图 一种简单定义FourCC常量的方法 (C/C++) [翻译]进化游戏的层次结构 - 用组件来重构你的游戏实体 [转载]Netmsg 局域网聊天程序 [转载]使用 WSAAsyncSelect 的 Winsock 编程模型 [转载]Singleton的一个基类实现 [转载] VC6 STLport-5.1.4 /STLport-4.6.2 编译,安装 [转载] 跨平台C++程序开发系列文章 计算机科学数学理论浅谈 (转载) 致初学作曲的业余音乐爱好者 (转载) 搜集的优良OpenGL教程 (转载) OpenGL教程 "Top Ten" (转载) fltk2更新简介 FLTK简介 开发者:我们应该在哪个层次编写代码? [Linux 书籍合集] Linux eBooks collection CodeLite可以媲美Code::Blocks 不指定
Project Euler Problem 17
HenryRead · 2012-09-21 · via 博客园 - HenryRead

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;
}