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.
1005 Spell It Right (20 分)代码如下:

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
#include<typeinfo>
using namespace std;

void Read(int t,bool space);
int main()
{
    string N;
    cin>>N;
    long long s = 0;
    int size = N.length();
    
    for(int i=0;i<size;i++)
    {
        int t = N[i] - '0';
        s += t;
    }
    stringstream ss;
    ss << s;
    ss >> N;
    
    size = N.length();
    int c = N[0] - '0';
    Read(c,false);
    
    for(int i=1;i<size;i++)
    {
        Read(N[i]-'0',true);
    }
    
    return 0;
}

void Read(int t,bool space)
{
    if(space)    cout<<" ";
    switch(t)
    {
        case 0:
            cout<<"zero";
            break;
        case 1:
            cout<<"one";
            break;
        case 2:
            cout<<"two";
            break;
        case 3:
            cout<<"three";
            break;
        case 4:
            cout<<"four";
            break;
        case 5:
            cout<<"five";
            break;
        case 6:
            cout<<"six";
            break;
        case 7:
            cout<<"seven";
            break;
        case 8:
            cout<<"eight";
            break;
        case 9:
            cout<<"nine";
            break;
    }
}
上一篇:PAT乙级-1005 继续(3n+1)猜想


下一篇:小A的通勤系列二——DP