首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【Pat】甲级1005 - Spell It Right(STL - map)

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

作者头像
FishWang
发布2025-08-26 14:25:58
发布2025-08-26 14:25:58
15000
代码可运行
举报
运行总次数:0
代码可运行

题目链接:点击打开题目


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的情况。


代码如下:

代码语言:javascript
代码运行次数: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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档