Project Euler Problem 17 asks you to find the total number of letters used when writing out the numbers from 1 to 1000 in English words.
For example, the number 342 is written as "three hundred and forty-two" and 115 as "one hundred and fifteen". Spaces and hyphens are not included in the letter count.
The problem is to determine the total number of letters used when writing out all the numbers from 1 to 1000 in English words.
This problem was more annoying than difficult because of the tedious task of counting letters in English words. It was especially frustrating to include the word "and," which we don't usually use in this context.
I kept getting the wrong answer, and after a while, I realized I had miscounted the letters. It turned out I had added an extra letter for the numbers 18 and 80. I had simply calculated 8 (eight) + 'teen' and 8 (eight) + 'ty' as 5+4 and 5+2, which was the mistake.
I didn't really want to solve this problem since I was planning to solve all problems from #1 onwards, but I ended up solving it anyway.
Here is the source code I wrote.
//------------------------------------------------
// Project Euler #17 - Number Letter Counts
// - by Aubrey Choi
// - created at 2015-01-14
//------------------------------------------------
#include <stdio.h>
int main()
{
int wc[20] = { 0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 };
int wct[20] = { 0, 0, 6, 6, 5, 5, 5, 7, 6, 6 };
int hundred = 7;
int thousand = 8;
int andword = 3;
int sum = 0;
for( int i = 1 ; i <= 1000 ; i++ )
{
int t = i/1000;
int h = (i/100)%10;
int s = (i%100>=20)?(i/10)%10:0;
int v = (i%100>=20)?i%10:i%100;
int r = 0;
if( t ) r += wc[t]+thousand;
if( h ) r += wc[h]+hundred;
if( s ) r += wct[s];
if( v ) r += wc[v];
if( (t | h) && (s | v) ) r += andword;
sum += r;
}
printf("Ans = %d\n", sum);
}
'Programming > Project Euler' 카테고리의 다른 글
[C/C++] Project Euler #19 Counting Sundays (0) | 2024.12.26 |
---|---|
[C/C++] Project Euler #18 Maximum Path Sum I(Dynamic Programming) (0) | 2024.12.26 |
[C/C++] Project Euler #16 Power Digit Sum(BigInteger) (0) | 2024.12.24 |
[C/C++] Project Euler #15 Counting fractions(Combination) (2) | 2024.12.23 |
[C/C++] Project Euler #14 Longest Collatz Sequence(Dynamic Programming) (2) | 2024.12.22 |