1. 程式人生 > >PAT 1005 Spell It Right

PAT 1005 Spell It Right

1005 Spell It Right (20 分)

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 (≤10​100​​).

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

水題

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#define memset(a,v) memset(a,v,sizeof(a))
using namespace std;
const int MAXN(1000);
const int INF(0x3f3f3f3f);
typedef long long int LL;
map<char,string>mp;
void init() {
    mp['0']="zero";
    mp['1']="one";
    mp['2']="two";
    mp['3']="three";
    mp['4']="four";
    mp['5']="five";
    mp['6']="six";
    mp['7']="seven";
    mp['8']="eight";
    mp['9']="nine";
}
char str[110];
int main() {
    init();
    while(~scanf("%s",str)) {
        int sum=0;
        for(int i=0;i<strlen(str);i++)
            sum+=(str[i]-'0');
        string res;
        while(sum)
            res+=(sum%10+'0'),sum/=10;
        if(res.empty()) {
            cout<<"zero"<<endl;
            continue;
        }
        cout<<mp[res[res.size()-1]];
        for(int i=res.size()-2;i>=0;i--)
            cout<<" "<<mp[res[i]];
        cout<<endl;
    }
}