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

推荐订阅源

Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
Webroot Blog
Webroot Blog
S
Security Affairs
H
Hacker News: Front Page
TaoSecurity Blog
TaoSecurity Blog
W
WeLiveSecurity
G
GRAHAM CLULEY
T
Tenable Blog
Schneier on Security
Schneier on Security
S
Securelist
Cyberwarzone
Cyberwarzone
P
Privacy International News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News
N
News and Events Feed by Topic
AWS News Blog
AWS News Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
D
DataBreaches.Net
博客园_首页
MyScale Blog
MyScale Blog
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
J
Java Code Geeks
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - 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;
}