題目:
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
解題思路:
各位數(shù)相加依次輸出對應(yīng)的單詞即可似舵。
注意一點——測試用例最高有100位數(shù)窝趣,long long也存放不下推正,可以用string存放。
代碼:
編譯器:C++(g++)
#include <iostream>
#include <string>
#include <unordered_map>
#include <deque>
using namespace std;
int main()
{
string n;
cin>>n;
if("0"==n)
{
cout<<"zero"<<endl;
return 0;
}
int sum=0;
while(!n.empty())
{
sum+=n.back()-'0';
n.pop_back();
}
unordered_map<int,string> itos;
itos[0]="zero";
itos[1]="one";
itos[2]="two";
itos[3]="three";
itos[4]="four";
itos[5]="five";
itos[6]="six";
itos[7]="seven";
itos[8]="eight";
itos[9]="nine";
deque<string> result;
while(sum!=0)
{
result.push_front(itos[sum%10]);
sum/=10;
}
for(int i=0;i!=result.size();++i)
{
if(i!=0)
{
cout<<" ";
}
cout<<result[i];
}
cout<<endl;
return 0;
}