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
| #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std;
typedef long long ll; vector<string> v1 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; vector<string> v2 = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string convertHundred(int num){ string res; int a = num / 100, b = num % 100, c = num % 10; res = b < 20 ? v1[b] : v2[b / 10] + (c ? " " + v1[c] : ""); if(a > 0) res = v1[a] + " Hundred" + (b ? " " + res : ""); return res; }
string numberToWords(ll num) { string res = convertHundred(num % 1000); vector<string> v = {"Thousand", "Million", "Billion", "Thousand Billion"}; for(int i = 0; i < 4; i++){ num /= 1000; res = num % 1000 ? convertHundred(num % 1000) + " " + v[i] + " " + res : res; } while(res.back() == ' ') res.pop_back(); return res.empty() ? "Zero" : res; }
int main() { int t; cin >> t; while(t--){ ll n; cin >> n; cout << numberToWords(n) << endl; } return 0; }
|