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

推荐订阅源

L
LINUX DO - 热门话题
T
The Blog of Author Tim Ferriss
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
D
Docker
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
雷峰网
雷峰网
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
美团技术团队
C
Cisco Blogs
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
C
CERT Recently Published Vulnerability Notes
S
Secure Thoughts
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
W
WeLiveSecurity
The Register - Security
The Register - Security
T
Tenable Blog
J
Java Code Geeks
The Cloudflare Blog
有赞技术团队
有赞技术团队
博客园 - 聂微东
P
Palo Alto Networks Blog
Security Latest
Security Latest
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
SegmentFault 最新的问题
H
Hacker News: Front Page
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
S
Security @ Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News: Ask HN
Hacker News: Ask HN

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