PAT 甲级 1082 Read Number in Chinese

PAT 甲级 1082 Read Number in Chinese

脑瘫写法,虽然能AC但是代码太丑了,明天补上参考答案。

#include<bits/stdc++.h>
using namespace std;
string num_in_chinese[10] = {
    "ling","yi","er","san","si","wu","liu","qi","ba","jiu"
};
string weishu_in_chinese[9] = {
    "","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"
};
int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#endif
    long long num; cin >> num;
    if (num < 0) cout << "Fu ";
    string num_str = to_string(abs(num));
    //倒序储存
    reverse(num_str.begin(), num_str.end());
    vector<string> ans;
    //先存起来,再按规律修改
    for (int i = 0; i < num_str.size(); ++i) {
        if (i == 0) ans.push_back(num_in_chinese[num_str[i] - '0']);
        else ans.push_back(num_in_chinese[num_str[i] - '0'] + " " + weishu_in_chinese[i]);
    }
    if (ans.size() == 1) {
        cout << ans[0];
        return 0;
    }
    if (ans.size() > 1 && ans[0] == "ling") ans[0] = "";
    if (ans.size() >= 2 && ans[1] == "ling Shi") {
        if (ans[0] == "") ans[1] = "";
        else ans[1] = "ling";
    }
    if (ans.size() >= 3 && ans[2] == "ling Bai") {
        if (ans[1] == "" || ans[1] == "ling") ans[2] = "";
        else ans[2] = "ling";
    }
    if (ans.size() >= 4 && ans[3] == "ling Qian") {
        if (ans[2] == "" || ans[2] == "ling") ans[3] = "";
        else ans[3] = "ling";
    }
    if (ans.size() >= 5 && ans[4] == "ling Wan") {
        ans[4] = "Wan";
    }
    if (ans.size() >= 6 && ans[5] == "ling Shi") {
        if (ans[4] == "Wan") ans[5] = "";
        else ans[5] = "ling";
        
    }
    if (ans.size() >= 7 && ans[6] == "ling Bai") {
        if (ans[5] == "" || ans[5] == "ling") ans[6] = "";
        else ans[6] = "ling";
    }
    if (ans.size() >= 8 && ans[7] == "ling Qian") {
        if (ans[6] == "" || ans[6] == "ling") ans[7] = "";
        else ans[7] = "ling";
        if (ans[7] == ""&&ans[6] == ""&&ans[5] == ""&&ans[4] == "Wan") ans[4] = "";
    }
    reverse(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); ++i) {
        if (i != 0 && ans[i] != "")cout << " ";
        cout << ans[i];
    }    
}

参考答案

#include<bits/stdc++.h>
using namespace std;
char num[10][5] = {
    "ling","yi","er","san","si","wu","liu","qi","ba","jiu"
};
char wei[5][5] = {
    "Shi","Bai","Qian","Wan","Yi"
};
int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);//freopen("output.txt", "w", stdout);
#endif
    char str[15];
    cin >> str;
    //left_ptr和right_ptr分别指向字符串首尾元素.
    int str_len = strlen(str);
    int left_ptr = 0, right_ptr = str_len-1;
    if (str[0] == '-'){
        printf("Fu");
        ++left_ptr;
    }
    //left_ptr和right_ptr指向同一节.
    while (left_ptr + 4 <= right_ptr) {
        right_ptr -= 4;
    }
    while (left_ptr < str_len) {
        //haves_pre_zero节中有前置零,have_printed节中已经输出过了.
        //前者用来控制只有一个零,后者用来控制输出“万”和“亿”.
        bool haves_pre_zero = false;
        bool have_printed = false;
        //一节一节处理.
        while (left_ptr <= right_ptr)
        {
            if (left_ptr > 0 && str[left_ptr] == '0') {
                haves_pre_zero = true;
            }
            else {
                if (haves_pre_zero == true) {
                    printf(" ling");
                    haves_pre_zero = false;
                }
                if (left_ptr != 0) printf(" ");
                printf("%s", num[str[left_ptr] - '0']);
                have_printed = true;
                //某节中除了最后一位,都要输出千百十.
                if (left_ptr != right_ptr) {
                    printf(" %s", wei[right_ptr - left_ptr - 1]);
                }
            }
            ++left_ptr;
        }
        if (have_printed&&right_ptr != str_len - 1) {
            printf(" %s", wei[(str_len - 1 - right_ptr) / 4 + 2]);
        }
        //继续处理下一节.
        right_ptr += 4;
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值