fork(10) download
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::string;
  5. using std::endl;
  6.  
  7. string toWords(int num) {
  8. if (num > 100 || num < 1) {
  9. throw "unsupported";
  10. }
  11. if (num == 100) {
  12. return "one hundred";
  13. }
  14.  
  15. const string kSpecialCases[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
  16. if (10 <= num && num <= 19) {
  17. return kSpecialCases[num - 10];
  18. }
  19.  
  20. const string kOnesPlaces[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
  21. const string kTensPlaces[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  22. if (num < 10) {
  23. return kOnesPlaces[num - 1];
  24. } else if (num % 10 == 0) {
  25. return kTensPlaces[num / 10 - 2];
  26. } else {
  27. return kTensPlaces[num / 10 - 2] + " " + kOnesPlaces[num % 10 - 1];
  28. }
  29. }
  30.  
  31. int main() {
  32. cout << toWords(1) << endl;
  33. cout << toWords(100) << endl;
  34. cout << toWords(12) << endl;
  35. cout << toWords(29) << endl;
  36. cout << toWords(46) << endl;
  37. return 0;
  38. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
one
one hundred
twelve
twenty nine
forty six