-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#017:Number_to_Words[projecteuler].cpp
More file actions
73 lines (64 loc) · 1.91 KB
/
#017:Number_to_Words[projecteuler].cpp
File metadata and controls
73 lines (64 loc) · 1.91 KB
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
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
string const digits[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
const string numbers[] = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
const string tens[] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
const string type[] = {"Trillion", "Billion", "Million" ,"Thousand", ""};
int n, k, j;
long tmp, x, nbr;
string str;
vector <int> v, v_sort, res;
cin >> n;
cout << endl;
for (int i = 0; i < n; i++)
{
cin >> x;
v.push_back(x);
}
v_sort = v;
sort (v_sort.begin(), v_sort.end());
x = 1;
j = 0;
while ((j < n) && !v_sort[j])
{
res.push_back(4);
j++;
}
for (; j < n; j++)
{
for (; x <= v_sort[j]; x++)
{
for (short i = 0; i < 5; i++) {
tmp = (x / (long)pow(10, 12 - i * 3));
if (!tmp)
continue ;
k = (tmp / 100) % 10;
if (k)
str.append(digits[k] + "Hundred");
if (k && (tmp % 100))
str.append("and");
k = (tmp / 10) % 10;
if (k == 1)
str.append(numbers[tmp % 10]);
else if (k)
str.append(tens[k]);
if ((k != 1) && (tmp % 10))
str.append(digits[tmp % 10]);
str.append(type[i]);
}
}
res.push_back(str.length());
}
// output
cout << endl;
for (int i = 0; i < n; i++)
{
auto it = find(v_sort.begin(), v_sort.end(), v[i]);
cout << res[distance(v_sort.begin(), it)] << endl;
}
return 0;
}