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 ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai

个人认为,这道题是我做到现在最难的一道题,并不是因为思路难,或者应用的东西难,而是情况很多,需要逐一考虑,非常锻炼思维,更能体会到一个好的架构有多么重要,如果刚开始的架构不好,中间的情况会非常复杂,很容易有遗漏,本题做了7个小时才完全AC,换了三种架构,太难了,欢迎大家体验~

#include<iostream>


#include<vector>

using namespace std;
string a[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
string b[6] = {"Shi", "Bai", "Qian", "Wan", "Yi", "Fu"};
int yi_part;
int wan_part;
int part;
string to_chinese(int num, bool flag){

    if(num == 0){
        return "";
    }
    string temp = to_string(num);
    if(num % 1000 == 0){

        return a[temp[0] - '0'] + " " + b[2];

    }else if(num % 100 == 0)
    {
        string s = "";
        if(num < 1000 && ((!flag && wan_part) || (flag && yi_part))){
            s = a[0] + " ";
        }
        for(int i = 0; i < temp.size() - 2; i++){

            s += a[temp[i] - '0'] + " " + b[temp.size() - 2 - i] + (i == temp.size() - 3 ? "" : " ");
        }
        
        return s;

    }else if(num % 10 == 0)
    {
        string s = "";
        if(num < 1000 && ((!flag && wan_part) || (flag && yi_part))){
            s = a[0] + " ";
        }
        for(int i = 0; i < temp.size() - 1; i++){

            if(temp[i] != '0'){
                s += a[temp[i] - '0'] + " " + b[temp.size() - 2 - i] + (i == temp.size() - 2 ? "" : " ");
            }else
            {
                s += a[temp[i] - '0'] + (i == temp.size() - 2 ? "" : " ");
            }
            

            
        }
        
        return s;

    }

        string temp_s = "";
        if(num < 1000 && ((!flag && wan_part) || (flag && yi_part))){
            temp_s = a[0] + " ";
        }
        for(int i = 0; i < temp.size() - 1; i++){

            if(temp[i] != '0'){
                temp_s += a[temp[i] - '0'] + " " + b[temp.size() - 2 - i] + " ";
            }else
            {
                if(temp[i + 1] != '0'){
                    temp_s += a[temp[i] - '0'] + " ";
                }

                    
            }
            
            
        }
        temp_s += a[temp[temp.size() - 1] - '0'];
        return temp_s;
    
    

}
int main(){
    
    int num;
    cin >> num;
    bool is_neg = false;
    if(num < 0){
        is_neg = true;
    }
    num = abs(num);
    yi_part = num / 100000000;
    wan_part = (num / 10000) % 10000;
    part = num % 10000;

    
    if(!yi_part && !wan_part && !part){
        cout << a[0] << endl;
    }else if(yi_part)
    {
        if(is_neg){
            cout << b[5] << " ";
        }
        if(wan_part){
            cout << a[yi_part] + " " + b[4] + " " + to_chinese(wan_part, true) + " " + b[3] + " " + to_chinese(part, false) << endl;
        }else
        {
            cout << a[yi_part] + " " + b[4] + " " + to_chinese(part, false) << endl;
        }
        
    }else
    {
        if(is_neg){
            cout << b[5] << " ";
        }
        if(wan_part){
            cout << to_chinese(wan_part, true) + " " + b[3] + " " + to_chinese(part, false) << endl;
        }else
        {
            cout << to_chinese(part, false) << endl;
        }
    }
    
    
    // cout << to_chinese(wan_part) << "-----" << to_chinese(part) << endl;


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值