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

来源:互联网 发布:戴勒姆波特知乎 编辑:程序博客网 时间:2024/05/30 04:13

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




原创粉丝点击