【PAT甲级】Spell It Right

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

解题思路:

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

AC代码: 

#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;
}

猜你喜欢

转载自blog.csdn.net/weixin_42449444/article/details/88782025