http://pat.zju.edu.cn/contests/pat-practise/1005

来源:互联网 发布:java.io jar包 编辑:程序博客网 时间:2024/04/30 18:20
 

 


1005. Spell It Right (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:
12345
Sample Output:
one five
反转字符串都要自己写!我了个去去去

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<cstdlib>  
  9. #include<string>  
  10. using namespace std;  
  11. #define MAX 10000000000  
  12. long long a[25];  
  13. char s[102];  
  14. char v[10][8] = {"zero""one""two""three""four""five""six""seven""eight""nine"};  
  15. string ltoa(long long x){  
  16.     string res;  
  17.     while(x){  
  18.         res += x%10 + '0';  
  19.         x /= 10;  
  20.     }  
  21.     int len = res.length();  
  22.     for(int i=0;i<len/2;++i){  
  23.         char c = res[i];  
  24.         res[i] = res[len-1-i];  
  25.         res[len-1-i] = c;  
  26.     }  
  27.     return res;  
  28. }  
  29. int main(){  
  30.   
  31.     //freopen("in.txt", "r", stdin);  
  32.     int j;  
  33.     while(cin>>s){  
  34.         memset(a, 0, sizeof(a));  
  35.         int len = strlen(s);  
  36.         for(int i=0;i<len;++i){  
  37.             a[0] += s[i] - '0';  
  38.             j = 0;  
  39.             while(a[j]>MAX){  
  40.                 a[j+1] += 1;  
  41.                 a[j] = a[j]%MAX;  
  42.                 j++;  
  43.             }  
  44.         }  
  45.         j = 15;  
  46.         while(j>=0 && a[j]==0){  
  47.             j--;  
  48.         }  
  49.         if(j==-1){  
  50.             printf("zero\n");  
  51.             continue;  
  52.         }  
  53.         bool first = true;  
  54.         while(j>=0){  
  55.             string str = ltoa(a[j]);  
  56.           
  57.             for(int i=0;i<str.length();++i){  
  58.                 if(first){  
  59.                     first = false;  
  60.                 }else{  
  61.                     printf(" ");  
  62.                 }  
  63.                 printf("%s", v[str[i]-'0']);  
  64.             }  
  65.               
  66.             j--;  
  67.         }  
  68.         printf("\n");  
  69.     }  
  70.     //fclose(stdin);  
  71.     return 0;  
  72. }  
原创粉丝点击