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
#include <cstdio>
#include <cstring>
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()
{
char str[11];
scanf("%s", str);
int len = strlen(str);
int left = 0, right = len - 1;
if(str[0] == '-'){
printf("Fu");
left++;
}
while(left + 4 <= right){
right -= 4; //首先将left和right移到同一节
}
while(left < len){ //从左到右,对每小节进行判断输出
bool flag = false; //用来判断是否需要输出ling
bool isprint = false; //用来判断当前小节是否有输出的数字,用来判断本小节输出完毕后是否需要输出Wan,Yi
while(left <= right){ //处理每小节,每小节有四位
if(left > 0 && str[left] == '0'){ //left > 0 处理了输入只有0的情况
flag = true;
} else {
if(flag == true){
printf(" ling"); //如果前面有累积的0,则输出ling,输出之后将flag标志置为false
flag = false;
}
if(left > 0) printf(" "); //只要不是首位就输出空格
printf("%s", num[str[left] - '0']); //输出当前left所指的位置
isprint = true; //此时说明该节已经有数字输出
if(left != right){ //每输出一位,判断输出十百千
printf(" %s", wei[right - left - 1]);
}
}
left++; //每输出一位,left加1
}
if(isprint == true && right != len - 1){ //输出Wan,Yi
printf(" %s", wei[(len - 1 - right) / 4 + 2]);
}
right += 4; //每小节输出完毕,right变为下一小节的最末尾
}
return 0;
}