【PAT甲级】Spell It Right

2019-11-08 13:40:42 浏览数 (1)

版权声明:本文为博主原创文章,遵循 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;
}

0 人点赞