甲級-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

解題思路:

各位數(shù)相加依次輸出對應(yīng)的單詞即可。
注意一點(diǎn)——測試用例最高有100位數(shù),long long也存放不下,可以用string存放。

代碼:

編譯器:C++(g++)

#include <iostream>
#include <string>
#include <unordered_map>
#include <deque>
using namespace std;

int main()
{
    string n;
    cin>>n;
    if("0"==n)
    {
        cout<<"zero"<<endl;
        return 0;
    }
    int sum=0;
    while(!n.empty())
    {
        sum+=n.back()-'0';
        n.pop_back();
    }
    unordered_map<int,string> itos;
    itos[0]="zero";
    itos[1]="one";
    itos[2]="two";
    itos[3]="three";
    itos[4]="four";
    itos[5]="five";
    itos[6]="six";
    itos[7]="seven";
    itos[8]="eight";
    itos[9]="nine";
    deque<string> result;
    while(sum!=0)
    {
        result.push_front(itos[sum%10]);
        sum/=10;
    }
    for(int i=0;i!=result.size();++i)
    {
        if(i!=0)
        {
            cout<<" ";
        }
        cout<<result[i];
    }
    cout<<endl;
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 題目 1005 Spell It Right (20 分)Given a non-negative integer...
    某翁閱讀 160評論 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,918評論 0 13
  • 或許因?yàn)槭巧钜?總是容易多愁善感 聽了幾首歌之后 以為已經(jīng)不會有觸動的心沒理由的感到寂寞 單身好多年幾乎已經(jīng)忘了有...
    懷蠢少女阿閱讀 280評論 1 1
  • 今天一直很慌毛概 一直有勁沒勁的背 后來都忘了帶了索性就算了 意外的出的題目大致都是我會的 有點(diǎn)小開心 有個重修的...
    磊寒嘛閱讀 162評論 0 0

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