前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】Spell It Right

【PAT甲级】Spell It Right

作者头像
喜欢ctrl的cxk
发布2019-11-08 13:40:42
3430
发布2019-11-08 13:40:42
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

代码语言:txt
复制
                 本文链接:[https://blog.csdn.net/weixin\_42449444/article/details/88782025](https://blog.csdn.net/weixin_42449444/article/details/88782025) 

Problem Description:

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 (≤

​​).

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:

代码语言:javascript
复制
12345

Sample Output:

代码语言:javascript
复制
one five

解题思路:

这算是甲级里面的水题吧。这种数字转换成拼音的题之前刷到过:【PAT乙级】1002. 写出这个数 【GPLT】L1-007 念数字。这题和乙级题很相似 给出的N都很大,区别就在于这题是需要将各位数上的数字进行相加求和再转换成拼音。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

void transform(int n)   //将数字转换成拼音
{
    switch(n)
    {
        case 0: cout << "zero"; break;
        case 1: cout << "one"; break;
        case 2: cout << "two"; break;
        case 3: cout << "three"; break;
        case 4: cout << "four"; break;
        case 5: cout << "five"; break;
        case 6: cout << "six"; break;
        case 7: cout << "seven"; break;
        case 8: cout << "eight"; break;
        case 9: cout << "nine"; break;
    }
}

int main()
{
    string str;
    cin >> str;
    int sum = 0;
    for(int i = 0; i < str.length(); i++)
    {
        int temp = str[i]-'0';
        sum += temp;
    }
    str = to_string(sum);
    bool isVirgin = true;   //判断是不是第一次
    for(int i = 0; i < str.length(); i++)
    {
        int temp = str[i]-'0';
        if(isVirgin)
        {
            transform(temp);
            isVirgin = false;
        }
        else
        {
            cout << " ";
            transform(temp);
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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