Commit 9102dfcc by Andrew Dahl

Solved Problem 17

parent b8bafd19
Showing with 79 additions and 0 deletions
Question:
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.
Answer: 21124
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> numbers;
int key = 0;
numbers[1] = 3;
numbers[2] = 3;
numbers[3] = 5;
numbers[4] = 4;
numbers[5] = 4;
numbers[6] = 3;
numbers[7] = 5;
numbers[8] = 5;
numbers[9] = 4;
numbers[10] = 3;
numbers[11] = 6;
numbers[12] = 6;
numbers[13] = 8;
numbers[14] = 8;
numbers[15] = 7;
numbers[16] = 7;
numbers[17] = 9;
numbers[18] = 8;
numbers[19] = 8;
numbers[20] = 6;
numbers[30] = 6;
numbers[40] = 5;
numbers[50] = 5;
numbers[60] = 5;
numbers[70] = 7;
numbers[80] = 6;
numbers[90] = 6;
numbers[1000] = 11;
for(int i = 21; i < 1000; i++)
{
key = i;
if((key < 100 && key %10 == 0))
continue;
numbers[i] = 0;
if(key >= 100)
{
numbers[i] = numbers[key / 100] + 7;
key = (key % 100);
if(i%100 != 0)
numbers[i] += 3;
else
continue;
}
if(key <= 20 || key % 10 == 0 || key > 100)
numbers[i] += numbers[key];
else
numbers[i] += numbers[key%10] + numbers[(key/10) * 10];
}
key = 0;
for(int i = 1; i <= 1000; i++)
{
key += numbers[i];
}
cout << key << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment