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

坚持

时间: 2024-12-21 12:52:47

PAT 1082. Read Number in Chinese的相关文章

PAT 1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25) 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

1082. Read Number in Chinese (25)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1082. Read Number in Chinese (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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

1082 Read Number in Chinese (25 分)

1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fufirst if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan li

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;

【PAT甲级】1082 Read Number in Chinese (25 分)

题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int num[17];string united[17]={"","yi","er","san&qu

1082. Read Number in Chinese (25)

题目如下: 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 b

A1082 Read Number in Chinese (25)(25 分)

A1082 Read Number in Chinese (25)(25 分) 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 B

PAT1082. Read Number in Chinese

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 Sh

Read Number in Chinese (25)

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