题目链接:点击打开题目
1005.Spell It Right (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
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
用map把0~9表示出来,然后输出就行了,注意要考虑和为0的情况。
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
typedef long long LL;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
int main()
{
map<int,string> d;
d[0] = "zero";
d[1] = "one";
d[2] = "two";
d[3] = "three";
d[4] = "four";
d[5] = "five";
d[6] = "six";
d[7] = "seven";
d[8] = "eight";
d[9] = "nine";
string num;
cin >> num;
int sum = 0;
for (int i = 0 ; i < num.size() ; i++)
sum += num[i] - '0';
stack<int> S;
if (sum == 0) //记得这个特判
{
cout << d[0] << endl;
return 0;
}
while (sum)
{
S.push(sum%10);
sum /= 10;
}
while (!S.empty())
{
cout << d[S.top()] << (S.size() == 1 ? '\n' : ' ');
S.pop();
}
return 0;
}