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
题目大意:
输入一组数字字符串,把每一位的数字求和结果用英文表示出来。
解题思路:
老实说这道题还是很良心的,只是用英文表达结果的每一位数字,而不是真正的读法,比如加hundred,thousand之类的就麻烦了。思路很简单,首先每位遍历,- ‘0’求和,再把求和结果转成字符串,按位输出为英文即可(要提前准备好英文输出的字母),代码如下:
#include<iostream>
#include<string>
using namespace std;
string english[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(){
string s;
int sum = 0;
cin >> s;
for(int i = 0; i < s.length(); i ++) {
sum += s[i] - '0';
}
string res = to_string(sum);
cout << english[res[0] - '0'];
for(int i = 1; i < res.length(); i ++) {
cout << " " << english[res[i] - '0'];
}
return 0;
}