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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<string.h> using namespace std; int num[20] = {0}, pt; void num2arr(int N){ pt = 0; do{ num[pt++] = N % 10; N = N / 10; }while(N != 0); } char strNum[10][10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; char duan[10][10] = {"", "Shi", "Bai", "Qian"}; char prt[50][10]; int main(){ int N, neg = 1, len = 0; scanf("%d", &N); if(N < 0){ neg = -1; N = N * -1; } num2arr(N); if(neg == -1){ strcpy(prt[len++], "Fu"); } int zeroTag = 0, wanTag = 0; if(N == 0){ printf("ling"); return 0; } for(int i = pt - 1; i>= 0; i--){ if(i == 8){ strcpy(prt[len++], strNum[num[i]]); strcpy(prt[len++], "Yi"); }else if(num[i] == 0){ zeroTag = 1; }else if(num[i] != 0){ if(i > 3 && i < 8){ wanTag = 1; } if(zeroTag == 1){ zeroTag = 0; strcpy(prt[len++], strNum[0]); } strcpy(prt[len++], strNum[num[i]]); if(i % 4 != 0) strcpy(prt[len++], duan[i % 4]); } if(i == 4 && wanTag == 1){ strcpy(prt[len++], "Wan"); } } for(int i = 0; i < len; i++){ if(i == len - 1) printf("%s", prt[i]); else printf("%s ", prt[i]); } cin >> N; return 0; }
总结:
1、属于字符串处理问题,我写的不太好,写完后才发现问题,于是只能修修补补勉强过了测试点。由于题目所给的数字最多才到亿,可以分成三段(如果够分的话),亿、万、个,分别处理。对于每一段,分别读数(几千几百几十几),读完每一段再分别输出‘亿’、‘万’。
2、对于零的处理:由于多个零可能按一个零读(1001),也可能不读(1000),这个需要额外注意。可以采取这样的办法:遇到0时不要立马读出,而是设置一个zero标志,该符号表示是否已经有了未读的0,在遇到一个非0的数字时,再检查zero标志看看是否有0未读,如果有则先读0,再读这个非0数字,再修改zero标志。 另外,在读完一段数字时,需要把zero标志置为没有未读0(如 1,1230,1234)。
3、注意N = 0时的处理。注意万段全为0时不可读“万”,且要读一个0(1,0000,1234)。
4、不好直接输出的,可以先拷贝到prt数组中,之后统一输出。