PAT(Advance) 1082. Read Number in Chinese (25)

这道题放了好久,一直不想做,感觉要考虑的情况好多。
实际做的时候感觉只要理清思路还是不难的。

#include 
#include 
#include 
using namespace std;
string num[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
map<int,string> digit={{10,"Shi"},{100,"Bai"},{1000,"Qian"},
                       {10000,"Wan"},{100000000,"Yi"}};
vector<string> read;
int main(){
    long n,cmp = 100000000;
    int index = 0;
    cin >>n;
    if(n<0){
        read.push_back("Fu");
        n = -n;
    }
    bool wan = false;
    int zero = 0;
    while(cmp != 0 && n / cmp == 0){
        cmp /= 10;
    }
    if(cmp == 0)
        read.push_back("ling");
    while(cmp){
        if(n/cmp){
            if(zero == 1)
                read.push_back("ling");
            zero = 0;
            read.push_back(num[n/cmp]);
            n -= (n/cmp)*cmp;
            if(cmp == 100000000)
                read.push_back(digit[100000000]);
            else if(cmp>10000) {
                read.push_back(digit[cmp / 10000]);
                wan = true;
            }
            else if(cmp == 1)
                continue;
            else read.push_back(digit[cmp]);
            if(cmp == 10000)
                wan = true;
        }
        else if(cmp == 10000&&wan == true){
            zero = 0;
            read.push_back("Wan");
        }
        else if(cmp == 10000){
            zero = 0;
            read.push_back("ling");
        }
        else zero++;
        cmp /= 10;
    }
    for(int i = 0;icout << read[i];
        if(i == read.size() - 1)
            cout << endl;
        else cout << ' ';
    }
    return 0;
}

你可能感兴趣的:(Pat)