1005 Spell It Right
分数 20
作者 CHEN, Yue
单位 浙江大学
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
读懂题目即可写,注意答案为0的时候需要特判一下。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int main()
{
map<int,string> mp = { {0,"zero"}, {1,"one"}, {2,"two"}, {3,"three"},
{4,"four"}, {5,"five"},{6,"six"},{7,"seven"},{8,"eight"},{9,"nine"} };
int n = 0;
string s;
cin >> s;
for(auto a : s)
n += a - '0';
vector<int> res;
while(n)
{
res.push_back(n % 10);
n /= 10;
}
if(res.size() == 0) //这儿得特判一下
puts("zero");
for(int i =res.size()-1 ; i>=0 ; --i)
{
cout << mp[res[i]];
if(i != 0)
cout << ' ';
else
cout << endl;
}
return 0;
}