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

模拟题。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

string num[15]=
{
    "ling","yi","er","san","si",
    "wu","liu","qi","ba","jiu"
};
string tmp="";
string ans="";
string s="";

string f(string TMP)
{
    if(TMP[0]=='0'&&TMP[1]=='0'&&TMP[2]=='0'&&TMP[3]=='0') return "ling";
    string t="";
    int p;
    for(int i=TMP.length()-1;i>=0;i--)
        if(TMP[i]!='0') {p=i; break;}

    for(int i=0;i<=p;i++) t=t+TMP[i];

    if(t==""  ) return "ling";

    if(t.length()==0) return "ling";
    if(t.length()==1) return num[t[0]-'0'];
    if(t.length()==2)
    {
        if(t[0]=='0') return num[t[1]-'0']+" Shi";
        return num[t[1]-'0']+" shi "+num[t[0]-'0'];
    }
    if(t.length()==3)
    {
        if(t[0]=='0'&&t[1]=='0') return num[t[2]-'0']+" Bai";
        if(t[0]=='0'&&t[1]!='0') return num[t[2]-'0']+" Bai "+num[t[1]-'0']+" Shi";
        if(t[0]!='0'&&t[1]=='0') return num[t[2]-'0']+" Bai ling "+num[t[0]-'0'];
        if(t[0]!='0'&&t[1]!='0') return num[t[2]-'0']+" Bai "+num[t[1]-'0']+" Shi "+num[t[0]-'0'];
    }
    if(t.length()==4)
    {
        if(t[0]=='0'&&t[1]=='0'&&t[2]=='0') return num[t[3]-'0']+" Qian";
        if(t[0]!='0'&&t[1]=='0'&&t[2]=='0') return num[t[3]-'0']+" Qian ling "+num[t[0]-'0'];
        if(t[0]=='0'&&t[1]!='0'&&t[2]=='0') return num[t[3]-'0']+" Qian ling "+num[t[1]-'0']+" Shi";
        if(t[0]!='0'&&t[1]!='0'&&t[2]=='0') return num[t[3]-'0']+" Qian ling "+num[t[1]-'0']+" Shi "+num[t[0]-'0'];
        if(t[0]=='0'&&t[1]=='0'&&t[2]!='0') return num[t[3]-'0']+" Qian "+num[t[2]-'0']+" Bai";
        if(t[0]!='0'&&t[1]=='0'&&t[2]!='0') return num[t[3]-'0']+" Qian "+num[t[2]-'0']+" Bai ling "+num[t[0]-'0'];
        if(t[0]=='0'&&t[1]!='0'&&t[2]!='0') return num[t[3]-'0']+" Qian "+num[t[2]-'0']+" Bai "+num[t[1]-'0']+" Shi";
        if(t[0]!='0'&&t[1]!='0'&&t[2]!='0') return num[t[3]-'0']+" Qian "+num[t[2]-'0']+" Bai "+num[t[1]-'0']+" Shi "+num[t[0]-'0'];
    }
}

int main()
{
    cin>>tmp;
    int len=tmp.length();
    for(int i=0;i<len/2;i++)
        swap(tmp[i],tmp[len-i-1]);

    if(tmp[tmp.length()-1]=='-') ans=ans+"Fu ";

    for(int i=0;i<tmp.length();i++)
    {
        if(tmp[i]=='-') continue;
        else s=s+tmp[i];
    }

    if(s.length()==9)
    {
        string s1,s2,s3;

        string g="";
        g=g+s[4]+s[5]+s[6]+s[7];
        s2=f(g);

        g="";
        g=g+s[0]+s[1]+s[2]+s[3];
        s3=f(g);

        ans=ans+num[s[s.length()-1]-'0']+" Yi";

        if(s2=="ling") {}
        else
        {
            ans=ans+" ";
            if(s[7]=='0') ans=ans+"ling ";
            ans=ans+s2;
            ans=ans+" Wan";
        }

        if(s3=="ling"){}
        else
        {
            ans=ans+" ";
            if(s[3]=='0') ans=ans+"ling ";
            ans=ans+s3;
        }
    }
    else if(s.length()>=5&&s.length()<=8)
    {
        string s2;
        string s3;

        int len=s.length();
        for(int i=len;i<8;i++) s=s+'0';

        string g;
        g=g+s[4]+s[5]+s[6]+s[7];
        s2=f(g);

        g="";
        g=g+s[0]+s[1]+s[2]+s[3];
        s3=f(g);

        ans=ans+s2;
        ans=ans+" Wan";

        if(s3=="ling"){}
        else
        {
            ans=ans+" ";
            if(s[3]=='0') ans=ans+"ling ";
            ans=ans+s3;
        }

    }
    else if(s.length()<=4)
    {
        string s3;

        int len=s.length();
        for(int i=len;i<4;i++) s=s+'0';

        string g;
        g=g+s[0]+s[1]+s[2]+s[3];
        s3=f(g);

        ans=ans+s3;
    }

    cout<<ans<<endl;

    return 0;
}

 

posted @ 2016-07-02 22:34  Fighting_Heart  阅读(149)  评论(0编辑  收藏  举报