题目:
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 (≤10^100).
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
解析:
输入一个位数小于等于101的数字,计算各位数字的累加和,并将累加和各位数字的对应英语输出。输出最后不能有多余空格。
答案:
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
string EngNum[10] = { "zero","one","two","three","four",
"five","six","seven","eight","nine" };
//各数字的英文字符串与数组下标一一对应,方便输出
void output(int sum)//递归输出结果
{
if (sum / 10)//不是最高位
{
output(sum / 10);
cout << " " << EngNum[sum % 10];
//输出空格和对应英文字符串
}
else
{
cout << EngNum[sum % 10];
//最高位输出对应字符串即可
}
}
int main()
{
char num;
int sum = 0;
while ((num = getchar()) != '\n')
//直接字符形式读取输入,不用考虑用什么类型来存储输入,避免溢出
{
sum = sum + num - '0';
}
output(sum);
}