【Pat】甲级1005 - Spell It Right(STL - map)

本文介绍了一个简单的程序设计问题 SpellItRight,任务是将一个非负整数的各位数字相加,并将得到的和用英文单词的形式输出。文章提供了完整的 C++ 实现代码,包括使用 map 映射数字到英文单词的方法,以及如何处理特殊情况。

题目链接:点击打开题目


1005.Spell It Right (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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


用map把0~9表示出来,然后输出就行了,注意要考虑和为0的情况。


代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
typedef long long LL;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
int main()
{
    map<int,string> d;
    d[0] = "zero";
    d[1] = "one";
    d[2] = "two";
    d[3] = "three";
    d[4] = "four";
    d[5] = "five";
    d[6] = "six";
    d[7] = "seven";
    d[8] = "eight";
    d[9] = "nine";
    string num;
    cin >> num;
    int sum = 0;
    for (int i = 0 ; i < num.size() ; i++)
        sum += num[i] - '0';
    stack<int> S;
    if (sum == 0)       //记得这个特判
    {
        cout << d[0] << endl;
        return 0;
    }
    while (sum)
    {
        S.push(sum%10);
        sum /= 10;
    }
    while (!S.empty())
    {
        cout << d[S.top()] << (S.size() == 1 ? '\n' : ' ');
        S.pop();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值