1005. Spell It Right (20)
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>
#include <unordered_map>
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
freopen("in.txt","r",stdin);
string s;
unordered_map<int,string> dict{make_pair(0,"zero"),make_pair(1,"one"),make_pair(2,"two"),make_pair(3,"three"),
make_pair(4,"four"),make_pair(5,"five"),make_pair(6,"six"),make_pair(7,"seven"),
make_pair(8,"eight"),make_pair(9,"nine")};
int sum=0;
stack<int> res;
cin>>s;
for(auto ch:s){
sum+=ch-'0';
}
while(sum){
res.push(sum%10);
sum/=10;
}
if(res.empty()){//sum为0
cout<<dict[0];
return 0;
}
cout<<dict[res.top()];
res.pop();
while(!res.empty()){
cout<<" "<<dict[res.top()];
res.pop();
}
return 0;
}
这道题只要注意到0的情况以及和为个位数的情况即可

本篇博客介绍了一道编程题目,任务是输入一个非负整数N,计算其所有数字之和,并将该和的每个数字用英文单词形式输出。博客提供了完整的C++代码实现,并解释了关键步骤,包括如何处理特殊情况。
502

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



