1005 Spell It Right (20 分)
时间限制: 400 ms 内存限制: 64 MB 代码长度限制: 16 KB
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
AC算法:
#include<iostream>
using namespace std;
int getValue(string n);
void Output(int v);
int main()
{
string n;
int value;
while(cin>>n)
{
value=getValue(n);
Output(value);
}
return 0;
}
int getValue(string n)
{
int sum,i;
sum=0;
for(i=0;i<n.size();i++)
{
int temp;
temp=n[i]-'0';
sum+=temp;
}
return sum;
}
void Output(int v)
{
string num[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
string ret;
ret="";
if(v==0)
{
ret="zero";
}
else
{
while(v!=0)
{
if(v/10==0)
{
ret=num[v%10]+ret;
}
else
{
ret=" "+num[v%10]+ret;
}
v=v/10;
}
}
cout<<ret<<endl;
}