题目例如以下:

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



题目要求依照中国人的习惯阅读一个不超过9位的数字。这道题的坑比較多,一定要考虑到全部的情况。

首先要抓住规律。我们能够发现,一个数字的读法在每一个4位是一致的。

比如12341234。我们读作“一千二百三十四一千二百三十四”,我们能够看到除去万字以外读法全然一致。

因此我们集中精力解决四位数的读法。然后加上亿、万等就可以。

在解决的时候,注意0的读法,比如1000读作一千。而1050读作一千五十,1005读作一千五。另外依据题目要求,10读作一十而不是十。

四位的读法分析:一次性传入4位,高位同意全0,由于要处理不同部位的4位。

①一位数直接读。读作零到九。

②两位数推断十位是否是0,假设是,而且个位不是0,设个位为x,应该读作零x,比如10,0005,传入0005,整个数应该读作十万零五。假设十位为0而且个位为0,则不读。比如10,0000。万已经由高4位处理完成,读作10万,低位不必读。

③三位数、四位数和两位数的思路一致,首先推断是否为0,假设发现当前位为0而且该位后面有不为0的,应该读一个零。注意不要读多了。

④普通情况仅仅须要依照不同的位先输出数字,然后输出Qian Bai Shi就可以,注意缩进处理。

把处理四位数的方法封装成一个函数。

整个数的处理方法为≤4位的直接用上面的函数。>4的截取不同的字符段来得到不同的位,每4位一截取。

代码例如以下:

#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <stdio.h>

using namespace std;

char* values[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};

int char2int(char c){
    return c - '0';
}

void handleNum(string num){

    int bits = num.length();

    bool printZero = false;
    switch(bits){
        case 1:{
            int ge = char2int(num[0]);
            printf("%s",values[ge]);
            break;
        }
        case 2:{
            int shi = char2int(num[0]);
            int ge = char2int(num[1]);
            if(shi != 0) printf("%s Shi",values[shi]);
            else if(!printZero){
                printf("ling");
                printZero = true;
            }
            if(ge != 0) printf(" %s",values[ge]);
            break;
        }
        case 3:{
            int bai = char2int(num[0]);
            int shi = char2int(num[1]);
            int ge = char2int(num[2]);
            if(bai != 0) {printf("%s Bai",values[bai]); printZero = false;}
            else if(!printZero && (shi !=0 || ge != 0)){
                printf("ling");
                printZero = true;
            }
            if(shi != 0) { printf(" %s Shi",values[shi]); printZero = false; }
            else if(!printZero && ge!=0){
                printf(" ling");
                printZero = true;
            }
            if(ge != 0) printf(" %s",values[ge]);
            break;
        }
        case 4:{
            int qian = char2int(num[0]);
            int bai = char2int(num[1]);
            int shi = char2int(num[2]);
            int ge = char2int(num[3]);
            if(qian != 0) {printf("%s Qian",values[qian]); printZero = false;}
            else if(!printZero && (bai!=0 || shi!=0 || ge!=0)){
                printf("ling");
                printZero = true;
            }
            if(bai != 0) {printf(" %s Bai",values[bai]); printZero = false;}
            else if(!printZero && (shi != 0 || ge != 0)){
                printf(" ling");
                printZero = true;
            }
            if(shi != 0) {printf(" %s Shi",values[shi]); printZero = false;}
            else if(!printZero && ge != 0){
                printf(" ling");
                printZero = true;
            }
            if(ge != 0) printf(" %s",values[ge]);
        }
    }

}

int main()
{
    string num;
    cin >> num;
    if(num[0] == '-'){
        num = num.substr(1);
        cout << "Fu ";
    }
    int bits = num.length();
    switch(bits){
    case 1:
    case 2:
    case 3:
    case 4:
        handleNum(num);
        break;
    case 5:{
        int wan = char2int(num[0]);
        printf("%s Wan ",values[wan]);
        handleNum(num.substr(1));
        break;
    }
    case 6:{
        handleNum(num.substr(0,2));
        printf(" Wan ");
        handleNum(num.substr(2));
        break;
    }
    case 7:{
        handleNum(num.substr(0,3));
        printf(" Wan ");
        handleNum(num.substr(3));
        break;
    }
    case 8:{
        handleNum(num.substr(0,4));
        printf(" Wan ");
        handleNum(num.substr(4));
        break;
    }
    case 9:{
        handleNum(num.substr(0,1));
        printf(" Yi ");
        handleNum(num.substr(1,4));
        printf(" Wan ");
        handleNum(num.substr(5));
        break;
    }
    }
    return 0;
}