[L1-007]念数字 | PAT GPLT团体程序设计天梯赛

Description

输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出“fu”字。十个数字对应的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu

Input

输入在一行中给出一个整数,如: 1234 。

提示:整数包括负数、零和正数。

Output

在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如 yi er san si。

Sample Input

-600

Sample Output

fu liu ling ling

题解

#include 
#include 
#include 
#include 
#include "iostream"
inline int tran(char i)
{
    return i - '0';
}
using namespace std;
int main(int argc, char const *argv[])
{
    string arr[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    string s;
    //= "0";
    cin >> s;
    int len = s.length();
    if (s[0] == '-')
    {
        printf("fu ");
        for (int i = 1; i < len; i++)
        {
            if (i <= len - 2)
                cout << arr[tran(s[i])] << " ";
            else
                cout << arr[tran(s[i])];
        }
    }
    else
    {       
        for (int i = 0; i < len; i++)
        {
            if (i <= len - 2)
                cout << arr[tran(s[i])] << " ";
            else
                cout << arr[tran(s[i])];
        }
    }
    return 0;
}

CC BY-SA 4.0 [L1-007]念数字 | PAT GPLT团体程序设计天梯赛 by 小小泥娃 is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.