PAT甲級-1005. Spell It Right (20)各位之和

kewlgrl發表於2018-02-18

1005. Spell It Right (20)

時間限制
400 ms
記憶體限制
65536 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

計算給出的數字的各個位上的數之和,並用英文形式輸出。

#include<bits/stdc++.h>
using namespace std;
#define INF 0xfffffff
#define MAXN 1010
string change(char x)
{
    if(x=='0') return "zero";
    else if(x=='1') return "one";
    else if(x=='2') return "two";
    else if(x=='3') return "three";
    else if(x=='4') return "four";
    else if(x=='5') return "five";
    else if(x=='6') return "six";
    else if(x=='7') return "seven";
    else if(x=='8') return "eight";
    return "nine";
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin>>s;
    long long len=s.length();
    long long ans=0;
    for(int i=0; i<len; ++i)
        ans+=(s[i]-'0');//各位之和
    string ss="";
    if(ans==0) ss+="0";
    while(ans>0)
    {
        ss+=(char(ans%10+'0'));//各位之和轉成字串
        ans/=10;
    }
    reverse(ss.begin(),ss.end());//翻轉字串
    len=ss.length();
    for(int i=0; i<len-1; ++i)
        cout<<change(ss[i])<<" ";
    cout<<change(ss[len-1])<<endl;
    return 0;
}

相關文章