美文网首页
甲级-1005 Spell It Right (20 分)

甲级-1005 Spell It Right (20 分)

作者: 梦终无痕_311d | 来源:发表于2019-09-27 17:00 被阅读0次

题目:

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

解题思路:

各位数相加依次输出对应的单词即可。
注意一点——测试用例最高有100位数,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;
}

相关文章

网友评论

      本文标题:甲级-1005 Spell It Right (20 分)

      本文链接:https://www.haomeiwen.com/subject/movlyctx.html