-
Notifications
You must be signed in to change notification settings - Fork 0
/
prb017.cpp
executable file
·141 lines (133 loc) · 2.51 KB
/
prb017.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
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 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.
*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int a,i,j,temp,count=0,counter,flag,flag2;
char s[50],number[10][7]={"One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
for(j=1;j<=1000;j++)
{
counter=0;
for(i=0;i<50;i++)
s[i]=0;
a=j;
if(a>100)
flag=1;
else
flag=0;
temp=a/1000;
if(temp>0)
{
strcat(s,number[temp-1]);
strcat(s,"Thousand ");
}
a=a%1000;
temp=a/100;
if(temp>0)
{
strcat(s,number[temp-1]);
strcat(s,"Hundred ");
}
a=a%100;
if(a<20 && a>9)
{
if(strlen(s)!=0)
strcat(s,"And ");
switch(a)
{
case 10:
strcat(s,"Ten ");
break;
case 11:
strcat(s,"Eleven ");
break;
case 12:
strcat(s,"Twelve ");
break;
case 13:
strcat(s,"Thirteen ");
break;
case 14:
strcat(s,"Fourteen ");
break;
case 15:
strcat(s,"Fifteen ");
break;
case 16:
strcat(s,"Sixteen ");
break;
case 17:
strcat(s,"Seventeen ");
break;
case 18:
strcat(s,"Eighteen ");
break;
case 19:
strcat(s,"Nineteen ");
break;
}
}
else
{
temp=a/10;
if(temp>0)
{
if(strlen(s)!=0)
{
strcat(s,"And ");
flag=0;
}
switch(temp)
{
case 2:
strcat(s,"Twenty ");
break;
case 3:
strcat(s,"Thirty ");
break;
case 4:
strcat(s,"Forty ");
break;
case 5:
strcat(s,"Fifty ");
break;
case 6:
strcat(s,"Sixty ");
break;
case 7:
strcat(s,"Seventy ");
break;
case 8:
strcat(s,"Eighty ");
break;
case 9:
strcat(s,"Ninety ");
break;
}
}
a=a%10;
if(a>0)
{
if(strlen(s)!=0 && flag)
strcat(s,"And ");
strcat(s,number[a-1]);
}
}
for(i=0;i<strlen(s);i++)
{
if(isalpha(s[i]))
counter++;
}
cout<<"\nFinal Result : "<<s<<"\tcounter="<<counter;
count=count+counter;
}
cout<<"\nTotal length : "<<count;
return 0;
}