#include <string> #include <vector> class ToString { vector<string> below20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; vector<string> tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; vector<string> thousands = {"", "Thousand", "Million", "Billion"}; //num <1000; string helper(int num) { if (num == 0) return ""; string res; if (num >= 100) { res += below20[num / 100] + " Hundred"; num %= 100; } if (num < 20) { if (!res.empty()) res += " "; res += below20[num]; return res; } if (!res.empty()) res += " "; res += tens[num / 10]; num %= 10; if (num) res += " " + below20[num]; return res; } public: string toString(int x) { // write code here if (x == 0) return "Zero"; string res; int i = 0; while (x > 0) { int cur = x % 1000; if (cur) { string part = helper(cur); if (!thousands[i].empty()) part += " "+ thousands[i] + ","; res = part + res; } x /= 1000; ++i; } while (!res.empty() && (res.back() == ' ' || res.back() == ',')) { res.pop_back(); } return res; } };