博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler Problem 17 Number letter counts
阅读量:6378 次
发布时间:2019-06-23

本文共 1668 字,大约阅读时间需要 5 分钟。

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.

C++:

#include 
#include
using namespace std;const int TWENTY = 20;const int HUNDRED = 28;const int ONE_THOUSAND = 29;string number[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred and", "one thousand"};int main(){ int sum=0, sum2=0; // 1 - 19 sum2 = 0; for(int i=1; i<=19; i++) sum2 += number[i].length(); sum += sum2; // 20 - 99 sum2 = 0; for(int i=20; i<=99; i++) { int d10 = i / 10, d0 = i % 10; sum2 += number[TWENTY + d10 - 2].length(); if(d0 != 0) sum2 += number[d0].length(); } sum += sum2; // 100 - 999 sum2 = 0; for(int i=1; i<=9; i++) { sum2 += (number[i].length() + number[HUNDRED].length() - 1) * 100 - 3; sum2 += sum; } sum += sum2; // 1000 sum += number[ONE_THOUSAND].length() - 1; cout << sum << endl; return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7564003.html

你可能感兴趣的文章
Spring Boot - how to configure port
查看>>
右键添加复制路径选项
查看>>
DocFetcher 本机文件搜索工具
查看>>
ambassador 学习三 限速处理
查看>>
HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
查看>>
数据结构:最小生成树--Kruskal算法
查看>>
Swift_1_基本数据类型
查看>>
深入解析Vuex实战总结
查看>>
流水落花春去也
查看>>
【教训】为什么不作备份?!
查看>>
ThinkPHP3.0启动过程
查看>>
JAX-WS(JWS)发布WebService
查看>>
Centos7安装docker-compse踩过的坑
查看>>
细说Nullable<T>类型
查看>>
oracle 插入表数据的4种方式
查看>>
7.Ajax
查看>>
Linux vi/vim编辑器常用命令与用法总结
查看>>
对于 url encode decode js 和 c# 有差异
查看>>
mysql 修改列为not null报错Invalid use of NULL value
查看>>
epoll源码分析
查看>>