【PAT】【Advanced Level】1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu". Note: zero ("ling") must be handled correctly according to the Chinese tradition. For example, 100800 is "yi Shi Wan ling ba Bai".

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai
原题链接:
https://www.patest.cn/contests/pat-a-practise/1082

https://www.nowcoder.com/pat/1/problem/4312

思路:

分解,判断,输出

CODE:

#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<cstdio>
#include<vector>
using namespace std;
 
string s[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
 
vector<string> op;
 
int main()
{
    int n;
    cin>>n;
    if (n<0)
    {
        n*=-1;
        if (n!=0)
            op.push_back("Fu");
    }
    int a=n/100000000;
    int b=n%100000000/10000;
    int c=n%10000;
    //cout<<a<<" "<<b<<" "<<c<<endl;
    if (a!=0)
    {
        op.push_back(s[a]);
        op.push_back("Yi");
    }  
    if (b!=0)
    {
        int b1=b/1000,b2=b%1000/100,b3=b%100/10,b4=b%10;
 
        if (b1!=0)
        {
            op.push_back(s[b1]);
            op.push_back("Qian");
        }
        else
        {
            if (a!=0) op.push_back("ling");
        }
        if (b2!=0)
        {
            op.push_back(s[b2]);
            op.push_back("Bai");
        }
        else
        {
            if (b1!=0&&!(b3==0&&b4==0))
                op.push_back("ling");
        }
        if (b3!=0)
        {
            op.push_back(s[b3]);
            op.push_back("Shi");
        }
        else
        {
            if (b2!=0&&b4!=0)
                op.push_back("ling");
        }
        if (b4!=0)
            op.push_back(s[b4]);
        op.push_back("Wan");
    }
    else
    {
        if (a!=0&&c!=0) op.push_back("ling");
    }
    if (c!=0)
    {
        int c1=c/1000,c2=c%1000/100,c3=c%100/10,c4=c%10;
        if (c1!=0)
        {
            op.push_back(s[c1]);
            op.push_back("Qian");
        }
        else
        {
            if (!(b==0)) op.push_back("ling");
        }
        if (c2!=0)
        {
            op.push_back(s[c2]);
            op.push_back("Bai");
        }
        else
        {
            if (c1!=0&&!(c3==0&&c4==0))
                op.push_back("ling");
        }
        if (c3!=0)
        {
            op.push_back(s[c3]);
            op.push_back("Shi");
        }
        else
        {
            if (c2!=0&&c4!=0)
                op.push_back("ling");
        }
        if (c4!=0)
            op.push_back(s[c4]);
    }
    if (a==0&&b==0&&c==0)
    {
        cout<<"ling";
    }
    else
    {
        cout<<op[0];
        for (int i=1;i<op.size();i++)
        {
            cout<<" "<<op[i];
        }
    }
     
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值