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

題目大意:
題目給定一個非負(fù)的整數(shù),要求把每一位上的數(shù)字相加,得到一個和,然后把這個和從高位到低位翻譯成英文單詞,1對應(yīng)one,2對應(yīng)two,3對three,0對應(yīng)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] << " ";
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 每個時代都有著每一個時代的特征,時光流轉(zhuǎn),都有著亙古不變的真理。強權(quán)勝,弱者敗。封建時代的官僚、皇權(quán)擁有著至高無...
    馨槾閱讀 636評論 7 4
  • 沉浮世間數(shù)十載,不過黃粱一場夢??v有千年鐵門檻,終須一個土饅頭。
    浮生若夢0403閱讀 119評論 0 0
  • 走過28年的匆匆歲月,終于懂得,生活中需要一種豁達和淡然,可以讓時光清淺;可以讓人只把人生當(dāng)做一次周而復(fù)始的循環(huán)過...
    劍南詩雪閱讀 939評論 0 2
  • 文 | 洪生鵬 1 做在班車?yán)锏却龓煾蛋l(fā)車時,窗外一位騎著單車的小伙子不小心車頭碰到了停在路邊的小轎車,小轎車的司...
    洪生鵬閱讀 970評論 0 4
  • 偶然發(fā)現(xiàn)了一組數(shù)字,36/64315/7/10。 我對數(shù)字不敏感,甚至可以說是遲鈍。 心血來潮地復(fù)習(xí)了一下小學(xué)算術(shù)...
    心中有茶閱讀 241評論 0 2

友情鏈接更多精彩內(nèi)容