您的位置:首页 > 其它

1005. Spell It Right (20)

2017-05-10 10:23 369 查看
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


题意:给你一个字符串,让你求和,并用英文 one two... 输出和的结果

思路:将所求的和sum一位位分离开,输出对应的英文就好

(这里我傻B的写错了一个英文单词,错了好几次才发现)

#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;

char digit[10][10] = { "zero","one" ,"two" ,"three","four" ,"five" ,"six" ,"seven" ,"eight" ,"nine" };
stack<int> ans;
int main()
{
char str[1000];
scanf("%s", str);
int len = strlen(str);
int sum = 0;
for (int i = 0; i < len; i++)
sum += str[i] - '0';
do//这里用do while 是为了处理 0 的特殊情况,不然有一组数据过不来
{
int d = sum % 10;
ans.push(d);
sum /= 10;
} while (sum > 0);
while (!ans.empty())
{
int t = ans.top();
if (ans.size() != 1)
printf("%s ", digit[t]);
else
printf("%s\n", digit[t]);
ans.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: