1005. Spell It Right (20)

来源:互联网 发布:初中物理知识网络 编辑:程序博客网 时间:2024/05/16 18:33

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]);elseprintf("%s\n", digit[t]);ans.pop();}return 0;}


0 0
原创粉丝点击