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

题目大意:
题目给定一个非负的整数,要求把每一位上的数字相加,得到一个和,然后把这个和从高位到低位翻译成英文单词,1对应one,2对应two,3对three,0对应zero,这样依次将每一位表示成一个单词,输出。

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
    stringstream strStream;
    vector<string> englishNum{ "zero","one","two","three","four","five","six","seven","eight","nine" };

    int num;
    cin >> num;
    string numstr=to_string(num);
    int count = 0;
    int temp;
    for (auto i = numstr.begin(); i !=numstr.end(); i++)
    {
        strStream << *i;
        strStream >> temp;
        strStream.clear();
        count += temp;
    }
    strStream.clear();
    string answer;
    strStream << count;
    strStream >> answer;
    for (size_t i = 0; i < answer.size(); i++)
    {
        strStream.clear();
        strStream << answer[i];
        strStream >> temp;
        
        if (i==answer.size()-1)
        {
            cout << englishNum[temp];
        }
        else
        {
            cout << englishNum[temp] << " ";
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 每个时代都有着每一个时代的特征,时光流转,都有着亘古不变的真理。强权胜,弱者败。封建时代的官僚、皇权拥有着至高无...
    馨槾阅读 588评论 7 4
  • 沉浮世间数十载,不过黄粱一场梦。纵有千年铁门槛,终须一个土馒头。
    浮生若梦0403阅读 102评论 0 0
  • 走过28年的匆匆岁月,终于懂得,生活中需要一种豁达和淡然,可以让时光清浅;可以让人只把人生当做一次周而复始的循环过...
    剑南诗雪阅读 852评论 0 2
  • 文 | 洪生鹏 1 做在班车里等待师傅发车时,窗外一位骑着单车的小伙子不小心车头碰到了停在路边的小轿车,小轿车的司...
    洪生鹏阅读 936评论 0 4
  • 偶然发现了一组数字,36/64315/7/10。 我对数字不敏感,甚至可以说是迟钝。 心血来潮地复习了一下小学算术...
    心中有茶阅读 202评论 0 2