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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<sstream>
#include<iostream>
#include<strstream>
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<<str<<" "<<str.length()<<endl;
	strlen=str.length();
}
int main()
{number num;
	int input,sum=0;
	for (  input=0;input<1000;)
	{
	num.value=input;
	num.subtract();
	input+=1;
	sum+=num.strlen;
	}
	cout<<sum;
	getchar();
	return 0;
}

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?
1
2
3
4
5
6
7
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:
1
2
3
4
5
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.