题目:
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
解题思路:
各位数相加依次输出对应的单词即可。
注意一点——测试用例最高有100位数,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;
}
网友评论