Project Euler: Problem 17 Number letter counts

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.

首先要知道数字对应的英文是什么

主要是考虑几种特殊的数字比如1-20 以及能被10整除的数,因为可以用一个单词表示出来。对于其它100以内的数可以可以用两个单词表示出来。

对于三位数,整百的可以用2个单词表示,其它的 X hundred and X (X) 这种结构。

四位数只有一个1000 :one thousand

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
	map<int, string>mp;
	mp[1] = "one";
	mp[2] = "two";
	mp[3] = "three";
	mp[4] = "four";
	mp[5] = "five";
	mp[6] = "six";
	mp[7] = "seven";
	mp[8] = "eight";
	mp[9] = "nine";
	mp[10] = "ten";
	mp[11] = "eleven";
	mp[12] = "twelve";
	mp[13] = "thirteen";
	mp[14] = "fourteen";
	mp[15] = "fifteen";
	mp[16] = "sixteen";
	mp[17] = "seventeen";
	mp[18] = "eighteen";
	mp[19] = "nineteen";
	mp[20] = "twenty";
	mp[30] = "thirty";
	mp[40] = "forty";
	mp[50] = "fifty";
	mp[60] = "sixty";
	mp[70] = "seventy";
	mp[80] = "eighty";
	mp[90] = "ninety";

	int len[1001];
	map<int, string>::iterator iter;
	for (iter = mp.begin(); iter != mp.end(); iter++)
	{
		len[iter->first] = iter->second.length();
	}

	for (int i = 21; i <= 99; i++)  // 35 thirteen five
	{
		if (i % 10 != 0)
		{
			int low = i % 10;
			int high = i - low;
			len[i] = len[low] + len[high];
		}
	}

	for (int i = 100; i <= 999; i++)
	{
		if (i % 100 == 0)   //700 seven hundred
		{
			int high = i / 100;
			len[i] = len[high] + 7;
		}
		else               //342 three hundred and fourty two
		{
			int high = i / 100;
			int low = i % 100;
			len[i] = len[high] + 7 + 3 + len[low];
		}
	}

	len[1000] = 11;

	int res = 0;
	for (int i = 1; i <= 1000; i++)
		res += len[i];
	cout << res << endl;
	system("pause");
	return 0;
}
时间: 2025-01-14 18:04:14

Project Euler: Problem 17 Number letter counts的相关文章

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet Description A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagore

Project Euler:Problem 12 Highly divisible triangular number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the fir

Project Euler: Problem 14 Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can b

Project Euler:Problem 11 Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30

Project Euler problem 68

题意需要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.并且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下就行. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctime> #include <set> #i

Project Euler problem 69

考察欧拉函数的一道题 首先要知道 [定理]正整数n(n≥2)可以唯一分解成素数乘积,即:n =p[1]^r1 * p[2] ^r2 * p[3]^r3. *...* p[s]^rs 其次欧拉函数有两个性质,可以用来编程,单独求phi函数: ① phi(m) =  m ( 1- 1/p[1]) ( 1- 1/p[2])-( 1- 1/p[s]) ② phi(p^k) = p^k – p^(k-1) = (p-1)p^(k-1) 然后 m/phi(m)  = (p[1] *p[2]*p[3]*

Project Euler: Problem 9 Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. 首先联立式子,消除c,得到关于ab的等式5

Project Euler Problem 675

ORZ foreverlasting聚聚,QQ上问了他好久才会了这题(所以我们又聊了好久的Gal) 我们先来尝试推导一下\(S\)的性质,我们利用狄利克雷卷积来推: \[2^\omega=I\ast|\mu|\] 这个很好理解吧,考虑一下它的组合意义即可 然后两边同卷上\(I\)有: \[2^\omega \ast I=I\ast I\ast |\mu|=d\ast |\mu|\] 后面还是同样,考虑\(d\ast |\mu|\)的组合意义,一正一反的情况下其实就是\(d(n^2)\) 因此我们

欧拉计划(python) problem 17

Number letter counts 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 w