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