码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1082. Read Number in Chinese

时间:2014-11-20 06:45:00      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   for   on   div   

#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

标签:style   blog   io   ar   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/lailailai/p/4109625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!