### Euler 17 :)

For sure most of us know www.projecteuler.net !! I can't solve this problem !!
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.
My Algorithm is So Easy Writing numbers in letters then count them
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include #include using namespace std; string numbers[10]={"","one","two","three","four","five","six","seven","eight","nine"}; string eleventonineteen[10]={"","eleven","tweleve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; string tennum[10]={"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; class number { public: int strlen; int value; void subtract(); private: void print(int hundred ,int ten,int value); }; void number::subtract() { int hundreds,tens; for ( hundreds=0;value>=100;++hundreds,value-=100); for ( tens=0;value>=10;++tens,value-=10); print(hundreds,tens,value); } void number::print(int hundred ,int ten,int value) { string str; if (hundred>0) str+=numbers[hundred]+"hundred"; if (hundred>0&&(ten>0||value>0)) str+="and"; if(ten==1&&value>0) str+=eleventonineteen[value]; else if (ten>0) { str+=tennum[ten]; if (value>0) str+=numbers[value]; } else if (value>0) str+=numbers[value]; cout<

I add The length of one thousand manually ( Lazy to edit the code )
But It doesn't work !!
Last edited on
Why not have one integer that stores the number of letters per word?

Also, what output are you currently getting? Which part of your program doesn't "work"? You need to provide more information rather than simply saying "but it doesn't work" if you don't want to wait hours for a response.

Well Phil the answer is not right It;s supposed to be 21124 and I got 21123 and after adding 11 ( The length of one thousand) It will be 21134
I can easily Put the answer in the box but I want to know what's wrong
Why?
 ``1234567`` ``````for ( input=0;input<1000;) { num.value=input; num.subtract(); input+=1; sum+=num.strlen; }``````

The instructions specifically say start at 1 and go to 1000, so your for loop should be:
 ``12345`` ``````for (input = 1; input < 1000; input ++) { num.value = input; num.subtract(); sum += num.strlen; }``````

And then add the value for 1000 afterwards.

As for why you're missing 1 number, I'm unsure, but I believe you have 10 extra numbers from something.

Edit: Tweleve is spelled wrong, it's Twelve. That should fix it since the teens occur exactly ten times.
Last edited on
Volatile Pulse I start From Zero it writes nothing :)
And You are right it was a spelling Mistake
Topic archived. No new replies allowed.