description
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
idea
- x yyyy zzzz
- x亿,若x不为0,直接输出
- yyyy只要其中有非零数,需要输出Wan,之间同样各十百千,个位无单位
- zzzz之间各十百千,个位无单位
- 零的处理,只输出两边是非零数的零,头尾的不输出
- 测试点:
- 测试点2是后半部分的零不输出,例如800800,后两位0不输出
solution
#include <stdio.h>
#include <string.h>
int main(){
char str[20], num[10][10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}, power[8][10] = {"Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi"};
int first = 0, zero = 0, yi = 0, wan = 0, wanS = 0, ge = 0, ling = 0, n = 0, end = 0, t;
gets(str);
if(str[0] == '-'){
printf("Fu ");
n++;
}
for(int i = (n > 0) ? 1 : 0; i < strlen(str); i++){
t = strlen(str) - i;
if(str[i] != '0'){
if(zero) {
if(yi || wanS || ge) {
printf(" ");
yi = wanS = ge = 0;
}
printf("ling");
ling = 1;
zero = 0;
}
if(!first) first = 1;
if(t == 9) {
printf("%s Yi", num[str[i] - '0']);
yi = 1; //后面有非零数时,再输出空格
}
else if(t < 9 && t > 4){
if(yi){
printf(" ");
yi = 0;
}
wan = 1;
if(t == 5) printf("%s ", num[str[i] - '0']);
else printf("%s %s ", num[str[i] - '0'], power[t - 6]);
}
else{
if(yi || wanS || ling || ge){
printf(" ");
yi = wanS = ge = ling = 0;
}
if(t != 1)printf("%s %s", num[str[i] - '0'], power[t - 2]);
else printf("%s", num[str[i] - '0']);
ge = 1;
}
}
else if(first && !zero){//不是首位数且第一次出现
zero = 1;//后续有非零数时输出0
}
if(t == 5 && wan) {
printf("Wan");
wan = 0;
wanS = 1;//后面有非零数时,再输出空格
}
}
if(!first) printf("ling");
return 0;
}
反思
理清逻辑后,还是有过不去的测试点的话,从是不是有低级错误的角度验证(例如本次zero更新了个寂寞……)