首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 1005 Spell It Right (20分)

PAT 1005 Spell It Right (20分)

作者头像
vivi
发布2020-07-14 10:44:40
2670
发布2020-07-14 10:44:40
举报
文章被收录于专栏:vblogvblog

题目

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 (≤10​100).

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

题目解读

题目很简单,给出一个正整数N,把它每个位置上的数组加起来得到一个新的整数M,要求输出M每个位置上的数字用对应的英文代替,并用空格隔开

也就是 123 要输出成 one two three ,并且最后面不能有多余空格。

注意: N 最大可以取到 10100,所以千万不要用 int,long long 。。。用 string !!!!

然后数字转成英文好办,用一个char数组作为映射表即可。

代码

题目比较简单,直接看吧,没什么难的,注意一下最后的输出末尾不要有多余空格。

#include <iostream>
using namespace std;

int main() {
    // int 无法存储
    string strNum;
    cin >> strNum;
    int len = strNum.length();
    int sum = 0;
    // 每一位加起来
    for (int i = 0; i < len; ++i) {
        sum += (strNum[i] - '0');
    }
    // 结果转为字符串,每一位用英文表示
    string strSum = to_string(sum);
    // 映射表
    string map[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    // 输出第一个位置
    cout << map[strSum[0] - '0'];
    // 输出 空格 其他位置,这样可以满足输出格式要求,最末尾不能有多余空格
    len = strSum.length();
    for (int i = 1; i < len; ++i) {
        cout << " " << map[strSum[i] - '0'];
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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