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:
1 | -123456789 |
Sample Output 1:
1 | Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu |
Sample Input 2:
1 | 100800 |
Sample Output 2:
1 | yi Shi Wan ling ba Bai |
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
题意
给出一串数字,要求输出中文读法。
分析
看到这题我的内心是自闭的🙄。不过兵来将挡,水来土掩,刚(头铁)就完事了。
一开始考虑用string存数字,便于提取各位。后来发现还是用int更方便。
首先,把数字分成三部分。第9位(亿级),8-5位(万级),4-1位(个级)。这里就能体现出int的优越性了,如果使用string会非常麻烦。然后依次处理。万级的逻辑与个级类似,只不过多加了个Wan。
start
用于控制开始输出,例如输入100。亿位万位都是0,显然不用输出0亿0万。当输出第一个不为0的数后,置start为1。
fisrt
用于控制空格。第一个数之前不输出空格, 其余数输出前需加空格
prtling
用于控制是否需要输出0。
- 亿级(第9位)
逻辑比较简单。判断是否为0即可。如果不为0,输出。如果万位个位全为0,直接返回。 - 万级(8-5位)
首先判断是否为0。如果为0输出ling。否则计算千百十个位,依次处理。
1.千位。还是判断是否为0,如果为0且百十个不都为0(在千位这个判断好像可以省去),则输出ling。否则在千位不等于0时,输出x Qian。(x为千位数字)
2.百位。与千位类似。判断是否为0,如果为0且十个位不为0,则输出ling(这个判断不能省,1001百位的0需输出,1000则不用,)。否则百位不为0时,输出x Bai。
3.十位个位逻辑类似。
4.最后输出Wan。 - 个级(4-1位)
逻辑与万级类似
代码
代码虽然丑了点,但是逻辑还是比较易于理解的。事实上,处理个十百千位的逻辑类似,处理万级与个级的逻辑也类似,还是可以把代码写的好看的,只不过得多花费些功夫。
柳婼大佬的代码,写的很简洁,但我是不可能看懂的🤷🏻♂️
1 |
|