首页 > 代码库 > PAT 1082. Read Number in Chinese

PAT 1082. Read Number in Chinese

#include <cstdio>#include <cstdlib>#include <string>#include <vector>#include <algorithm>using namespace std;const char* d2cn[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};const char* t2cn[] = {"Ge", "Shi", "Bai", "Qian"};void num2cn(int num, vector<string>& out) {    vector<string> tmp;    int i = 0;    int last = 0;    while (num) {        int cur = num % 10;        if (cur != 0 || last != 0) {            if (i != 0 && cur != 0) {                tmp.push_back(string(t2cn[i]));            }            tmp.push_back(string(d2cn[cur]));        }        last = cur;        num /= 10;        i++;    }    reverse(tmp.begin(), tmp.end());    out.insert(out.end(), tmp.begin(), tmp.end());}int main() {    vector<string> out;    int N;    scanf("%d", &N);    int num = N;    if (num < 0) {        out.push_back("Fu");        num = -num;    }        int ds[9] = {0};    int i = 0;    int yi  = num / 100000000;    num = num % 100000000;    int wan = num / 10000;    int ge  = num % 10000;        if (yi != 0) {        out.push_back(d2cn[yi]);        out.push_back("Yi");    }    if (wan != 0) {        if (wan < 1000 && yi != 0) {            out.push_back("ling");        }        num2cn(wan, out);        out.push_back("Wan");        }        if (ge != 0) {        if (ge < 1000 && (yi | wan)) {            out.push_back("ling");        }        num2cn(ge, out);    }    if (out.size() == 0) {        printf("ling");    } else {        printf("%s", out[0].c_str());        int len = out.size();        for (int i=1; i<len; i++) {            printf(" %s", out[i].c_str());        }    }    printf("\n");    return 0;}

坚持

PAT 1082. Read Number in Chinese