Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
Professor Tom is teaching postgraduates a course on genes. One thing gives him a headache: there are thousands of base pairs on a chromosome, which number from 0 to millions, tens of millions, or even hundreds of millions.
For example, when explaining the base at position 1234567009 to students, it is difficult to read the numbers accurately just by looking at them.
Therefore, he urgently needs a system, and then when he enters 12 3456 7009, he will give the corresponding reading method:
1.234.56709 billion
Expressed in Chinese Pinyin as
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
So he just needs to read it.
Your task is to help him design such a system: given an Arabic numeral string, you help him convert it into a Chinese Pinyin string according to the Chinese reading and writing norms, and the two adjacent syllables are separated by a space character.
Note that the specifications must be strictly followed. For example, "10010" reads "yi wan ling yi shi" instead of "yi wan ling shi", "100000" reads "shi wan" instead of "yi shi wan", and "2000" reads "er qian" instead of "liang qian".
Input format
There is a number string with a value size of no more than 2000000000.
Output format
Is a string composed of lowercase English letters, commas and spaces, indicating the English pronunciation of the number.
sample input
1234567009
sample output
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
My analysis:
Because it is difficult to determine the highest bit of the number, it is difficult to traverse from the last bit of the string, that is, a bit. Then, because we need to output the highest bit first and deal with the lowest bit first, we can use the stack to store Pinyin. The Pinyin of the lowest bit will be put into the stack first, and then the Pinyin of the highest bit will be put out of the stack first. Here, I'm going to deal with one digit by one, so flag means that the current digits, such as individual bits and one billion bits, are relatively simple to deal with, so they can be dealt with separately. Then, both ten and 100000 bits need to consider the problems of 100000 and 100000 (plus or not plus one), so they can be dealt with alone. Then, other digital processing is basically the same. Even if there are different, they are individual statements. I use if to process them, so I share a code block for them. Another key issue is the addition of zero. When a digit is followed by all zeros, there is no need to add zeros. If it is followed by zeros but not all bits are zero, you need to add zeros. Here I define the zeroFlag variable to deal with this problem, which indicates how many zeros are followed by the current digit.
import java.util.Scanner; import java.util.Stack; class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] pinyin = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu", "shi", "bai", "qian", "wan" }; String num = in.next(); Stack<String> result = new Stack<String>(); int zeroFlag = 0; int flag = 1; for (int i = num.length() - 1; i > -1; i--, flag++) switch (flag) { case 1: if (num.charAt(i) != 48) result.push(pinyin[num.charAt(i) - 48]); else zeroFlag = 1; break; case 2: if (num.charAt(i) != 48) { zeroFlag = 0; if (num.length() == 2 && num.charAt(i) == 49) { result.push(pinyin[10]); } else { result.push(pinyin[10]); result.push(pinyin[num.charAt(i) - 48]); } } else { zeroFlag++; } break; case 3: case 4: case 5: case 7: case 8: case 9: if (num.charAt(i) != 48) { if (zeroFlag < flag - 1 && zeroFlag > 0) result.push(pinyin[0]); zeroFlag = 0; if (flag < 5) result.push(pinyin[flag + 8]); else if (flag < 9) { if (flag == 5) result.push(pinyin[13]); else result.push(pinyin[flag + 4]); } else { result.push(pinyin[1]); } result.push(pinyin[num.charAt(i) - 48]); } else { switch (flag) { case 5: result.push(pinyin[13]); break; case 9: result.push(pinyin[1]); } zeroFlag++; } break; case 6: if (num.charAt(i) != 48) { if (zeroFlag < flag - 1 && zeroFlag > 0) result.push(pinyin[0]); zeroFlag = 0; if (num.length() == 6 && num.charAt(i) == 49) { result.push(pinyin[10]); } else { result.push(pinyin[10]); result.push(pinyin[num.charAt(i) - 48]); } } else { zeroFlag++; } break; case 10: if (zeroFlag < flag - 1 && zeroFlag > 0) result.push(pinyin[0]); if (num.charAt(i) == 49) { result.push(pinyin[10]); } else { result.push(pinyin[10]); result.push(pinyin[num.charAt(i) - 48]); } } int mark = 1; while (!result.empty()) if (mark == 1) { System.out.print(result.pop()); mark = 0; } else { System.out.print(" " + result.pop()); } in.close(); } }