#P2555. Chinese Number Reading
Chinese Number Reading
Chinese Number Reading
Sealock, who was born overseas, finds reading numbers in Chinese very challenging. Little Keke wants to help him read any number whose absolute value is less than one billion. The rules for reading a number are as follows:
- Overall Structure: A number may have an optional sign, an integer part, an optional decimal point and a decimal part. If a decimal point is present then a decimal part must follow.
- Sign: For a positive number, whether a '+' is present or not, no sign is read. For a negative number, the leading '-' is read as
F
(standing for "负"). - Integer Part:
- If the integer part is missing or consists only of zeros, read it as
0
. - Otherwise, ignore any leading zeros and read from the first nonzero digit. The reading uses a set of quantity units. For numbers whose total integer digits are from 2 to 4, the first digit’s unit is determined by the length: for 2-digit numbers use
S
(ten), for 3-digit numbers useB
(hundred), for 4-digit numbers useQ
(thousand). - If the integer part has more than 4 digits, split it into two parts: the left part (all but the last 4 digits) and the right part (the last 4 digits). The left part is processed as above and then appended with the group unit
W
(representing ten-thousands), and the right part is converted to a number and formatted with at least two digits (padding with a leading zero if necessary). For example, the integer part "20030004" is split into "2003" and "0004". Processing "2003" yields "2Q03" (explained below) and processing "0004" gives "04", so the final integer reading is "2Q03W04". - Within any group (when processed as: remove leading zeros and, if the group has at least 2 digits, append a unit to the first digit):
- If the group has only one digit, output it directly.
- If the group has length ≥ 2, then the first (leftmost) digit when nonzero is output and appended with a unit. The unit is chosen by the length: 2-digit group uses
S
, 3-digit usesB
, 4-digit usesQ
. - Then process the remaining digits from left to right. For a contiguous sequence of zeros that is surrounded by nonzero digits, output it as a single
0
; however, if a block of zeros occurs at the very beginning or end of the group, it is not read.
- If the integer part is missing or consists only of zeros, read it as
- Decimal Part: If the number has a decimal part, first output
D
(for "点") then read each digit of the decimal part in order. Note that even consecutive zeros are read individually. If there is a decimal point without any digits after it, do not outputD
.
Examples:
- The input
-0020030004.567
should be read asF2Q03W04D567
. - The input
000.89
should be read as0D89
.
Your task is to write a program to help read any given number according to the above rules.
inputFormat
The input consists of a single line containing a string which represents the number. The string may include an optional '+' or '-' sign, a sequence of digits for the integer part (possibly with leading zeros), an optional decimal point and a sequence of digits for the decimal part. The absolute value of the number is less than 109.
outputFormat
Output the reading of the given number as a single string, according to the rules described above.
sample
-0020030004.567
F2Q03W04D567