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:12345Sample Output:
one five
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cstdio>
#include <algorithm>
using namespace std;
vector<int> parse(string s){
vector<int> result;
for(auto& ch : s){
result.push_back(ch - '0');
}
return result;
}
unordered_map<int, string> mapping = {
{0, "zero"},
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"}
};
bool first = true;
void print(long num){
if(num == 0) return;
int n = num % 10;
print(num / 10);
if(first) first = false;
else printf(" ");
printf("%s", mapping[n].c_str());
}
int main(){
string s;
cin >> s;
auto result = parse(s);
long sum = accumulate(result.begin(), result.end(), 0);
if(sum == 0) printf("zero");
else print(sum);
return 0;
}
本文详细解析了一个将非负整数转换为其英文单词形式的算法,包括输入和输出规范,以及核心实现步骤。
343

被折叠的 条评论
为什么被折叠?



