题目链接:http://www.patest.cn/contests/pat-a-practise/1005
题面:
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 (<= 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:12345Sample Output:
one five
题目大意:
将各位数相加,再将得到的结果,一位一位用英文输出。
解题:
模拟就好,注意0的情况。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int main()
{
string s,ans;
cin>>s;
map <int,string> refl;
//映射关系
refl[0]="zero";
refl[1]="one";
refl[2]="two";
refl[3]="three";
refl[4]="four";
refl[5]="five";
refl[6]="six";
refl[7]="seven";
refl[8]="eight";
refl[9]="nine";
int sum=0;
//相加
for(int i=0;i<s.length();i++)
sum+=(s[i]-'0');
while(sum)
{
ans+=(sum%10+'0');
sum/=10;
}
//输出
if(ans.length()==0)
cout<<"zero";
else
{
cout<<refl[ans[ans.length()-1]-'0'];
for(int i=ans.length()-2;i>=0;i--)
cout<<" "<<refl[ans[i]-'0'];
}
cout<<endl;
return 0;
}