-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem_#17_Number_to_Words.cpp
64 lines (61 loc) · 1.6 KB
/
Problem_#17_Number_to_Words.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
string units[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"};
string decim[] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string hun = "Hundred";
string thous = "Thousand";
string mill = "Million";
string bill = "Billion";
vector<string> ans;
void solve(ll n){
if(n <= 20){
ans.push_back(units[n]);
}else if(n < 100){ // > 20
ans.push_back(decim[n/10]);
solve(n%10);
}else if(n < 1000){// >= 100
solve(n/100);
ans.push_back(hun);
solve(n%100);
}else if(n < 1e6){ // >= 1000
solve(n/1000);
ans.push_back(thous);
solve(n%1000);
}else if(n < 1e9){
ll h = 1e6;
solve(n/h);
ans.push_back(mill);
solve(n%h);
}else{
ll h = 1e9;
solve(n/h);
ans.push_back(bill);
solve(n%h);
}
}
int main() {
int t; scanf("%d", &t);
while(t--){
ll n; scanf("%lld", &n);
if(n == 0){
cout << "Zero\n";
}
else{
ans.clear();
solve(n);
cout << ans[0];
for(int i=1;i<ans.size();i++){
if(ans[i] == "")continue;
cout << " "<<ans[i];
}
cout << "\n";
}
}
return 0;
}