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

解题思路:

各位数相加依次输出对应的单词即可。
注意一点——测试用例最高有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 分)Given a non-negative integer...
    某翁阅读 120评论 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,504评论 0 13
  • 或许因为是深夜 总是容易多愁善感 听了几首歌之后 以为已经不会有触动的心没理由的感到寂寞 单身好多年几乎已经忘了有...
    怀蠢少女阿阅读 262评论 1 1
  • 今天一直很慌毛概 一直有劲没劲的背 后来都忘了带了索性就算了 意外的出的题目大致都是我会的 有点小开心 有个重修的...
    磊寒嘛阅读 142评论 0 0