fork download
  1. #include<bits/stdc++.h>
  2. #include<string.h>
  3. using namespace std;
  4. string s;
  5. string a[33]={"negative","one", "two", "three", "four", "five",
  6. "six","seven", "eight", "nine", "ten","eleven",
  7. "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
  8. "seventeen", "eighteen","ninenteen","20","21","twenty","thirty",
  9. "forty","fifty","sixty","seventy","eighty","ninety"};
  10. int check(string s,string a[]){
  11. for(int i=1;i<=29;i++)
  12. if(s==a[i]) return i;
  13. }
  14. int main(){
  15. int dem=0,dat=0,cnt=0;
  16. string x="";
  17. string luu="";
  18. while(cin >> s){
  19. int j=check(s,a);
  20. if(s==a[0]){
  21. cout << "-";
  22. continue;
  23. }
  24. else if(s=="million"){
  25. if(dat==1) while(x.length()<3) x='0'+x;
  26. cout << x ;
  27. x="";
  28. dem=0;
  29. dat=1;
  30. cnt=1;
  31. continue;
  32. }
  33. else if(s=="thousand"){
  34. if(dat==1) while(x.length()<3) x='0'+x;
  35. cout << x ;
  36. x="";
  37. dem=0;
  38. dat=1;
  39. cnt=0;
  40. continue;
  41. }
  42. else if(s=="hundred"){
  43. x+="00";
  44. //cout << x << endl ;
  45. dem=3;
  46. continue;
  47. }
  48. else if(j>=1 && j<=9){
  49. luu=(char)(j + 48);
  50. if(x!=""){
  51. x.pop_back();
  52. x += luu;
  53. //cout << x << endl ;
  54. }
  55. else x=luu;
  56. //dem=1;
  57. //cout << x << endl ;
  58. }
  59. else if(j>=10 && j<=19){
  60. luu=(char)(j + 38);
  61. if(dem==3){x.pop_back(); x.pop_back();
  62. }
  63. x+='1'+luu;
  64. }
  65. else if(j>=22 && j<=29){
  66. luu=(char)(j - 20 + 48);
  67. if(dem==3){x.pop_back(); x.pop_back();
  68. }
  69. x+=luu+'0';
  70. //cout << x << endl ;
  71. //dem=2;
  72. }
  73. //cout << x << endl << luu << endl ;
  74. }
  75. if(cnt==1) cout << "000" ;
  76. if(dat==1) while(x.length()<3) x='0'+x;
  77. cout << x ;
  78. }
  79. // negative one million seven hundred twenty nine thousand one hundred one
Success #stdin #stdout 0.01s 5320KB
stdin
one hundred thousand twenty one
stdout
100021