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
题目意思很简单,就是对每一位数求和后得到的数用字母一位一位的表示即可,注意首位不可以为0
#include<iostream>
#include<string>
#include<map>
using namespace std;
map < int,string > m;
void init()
{
m[1]="one";
m[2]="two";
m[3]="three";
m[4]="four";
m[5]="five";
m[6]="six";
m[7]="seven";
m[8]="eight";
m[9]="nine";
}
void output(string str)
{
int len=str.length();
int flag=0;
if(str=="0")
{
cout<<"zero";
return ;
}
for(int i=0;i<len;i++)
{
if(i!=0&&str[i]=='0')
{
cout<<" zero";
continue;
}
if(flag==0)
{
cout<<m[str[i]-'0'];
flag=1;
}
else cout<<" "<<m[str[i]-'0'];
}
}
int main()
{
init();
string str;
cin>>str;
int sum;
int len=str.length();
for(int i=0;i<len;i++)
{
sum+=(str[i]-'0');
}
string s=to_string(sum);
output(s);
return 0;
}
其实我这写的初始化函数其实可以用一个数组存,不用map其实更加简单,注意一下0的处理,一般大整数都用string存就可以解决问题