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
之前做的时候取巧了,因为输入小于10^100,故输入数字的各个位数之和不会超过10×100 = 1000,即最多是一个三位的数字。
本题很简单,注意下输入为”0”的时候的输出,即可。
当然如果
#include <string>
string spell[13] = {“zero”, “one”, “two”, …};
可以使输出过程简化很多。
#include <iostream>
using namespace std;
// N < 10^100, sum < 10*100 = 1000, sum最大为三位数
void spell(const int &num)
{
switch(num)
{
case 0:
cout << "zero";
break;
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
case 5:
cout << "five";
break;
case 6:
cout << "six";
break;
case 7:
cout << "seven";
break;
case 8:
cout << "eight";
break;
case 9:
cout << "nine";
break;
default:
cout << "error";
break;
}
}
int main()
{
char ctemp;
int ntemp = 0;
int sum = 0;
int flag = 0; // 用于控制输出格式
ctemp = cin.get();
while (ctemp != '\n')
{
sum += ctemp - '0';
ctemp = cin.get();
}
ntemp = sum/100;
if (0 != ntemp)
{
spell(ntemp);
cout << ' ';
flag = 1;
}
sum = sum - ntemp*100;
ntemp = sum/10;
if (1 == flag || 0 != ntemp)
{
spell(ntemp);
cout << ' ';
flag = 1;
}
ntemp = sum - ntemp*10;
spell(ntemp);
// system("pause");
return 0;
}