fork(10) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<string> under20 = {"Zero", "One", "Two", "Three", "Four", "Five",
  6. "Six", "Seven", "Eight", "Nine", "Ten",
  7. "Eleven","Twelve", "Thirteen", "Fourteen", "Fifteen",
  8. "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"};
  9.  
  10. vector<string> tens = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
  11. "Sixty", "Seventy", "Eighty", "Ninety"};
  12.  
  13. string numBelow1000(int num) {
  14. if(num == 0) return under20[0];
  15. string ret = "";
  16. if (num > 100) {
  17. ret = under20[num/100] + " Hundred";
  18. num %= 100;
  19. if (num == 0) {
  20. return ret;
  21. } else {
  22. ret += " and ";
  23. }
  24. }
  25.  
  26. if (num <= 20) {
  27. ret += under20[num];
  28. } else {
  29. ret += tens[num/10];
  30. num %= 10;
  31. if (num > 0)
  32. ret += " " + under20[num];
  33. }
  34. return ret;
  35. }
  36.  
  37. string num2string(int num) {
  38. int part1 = num / 1000;
  39. int part2 = num % 1000;
  40. string res;
  41. if(part1) {
  42. res = numBelow1000(part1) + " Thousand";
  43. if(part2) res += ", " + numBelow1000(part2);
  44. } else {
  45. res = numBelow1000(part2);
  46. }
  47. return res;
  48. }
  49.  
  50. int main() {
  51. int num = 0;
  52. cin >> num;
  53. string rs = num2string(num);
  54. cout<<rs<<endl;
  55. return 0;
  56. }
Success #stdin #stdout 0s 3280KB
stdin
4618
stdout
Four Thousand, Six Hundred and Eighteen