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
-
题目大意:
这是一个简单题,给出一个非负整数,求得各位数字之和,并转换成英文输出; -
思路:
- 由于给出的非负整数超过了长整型范围,因此作为字符串读入,再逐位转成数字进行运算;
- 运算结果再转换成英文;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, ans;
int sum = 0;
string book[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cin >> s;
for (int i = 0; i < s.size(); i++)
{
sum += s[i] - '0';
}
ans = to_string(sum);
for (int i = 0; i < ans.size(); i++)
{
cout << book[ans[i] - '0'];
if (i < ans.size() - 1)
{
cout << ' ';
}
}
return 0;
}