Cup Blue Bridge Group B @java basic training exercises (30) BASIC-020: number of readings, Keywords: function judgment, the printf usage array

Cup Blue Bridge Group B @java basic training exercises (30) BASIC-020: number of readings, Keywords: function determines

Reference Links:
https://blog.csdn.net/ITmincherry/article/details/105315333

https://blog.csdn.net/qq_42286193/article/details/105081870

Problem Description

Professor Tom is to teach a graduate course on genes, one thing made him quite a headache: there are tens of thousands of base pairs on a chromosome, they are numbered from 0 to millions, tens of millions, or billions.
  For example, when the students about the bases at position No. 1234567009, the light looks figures are difficult to accurately read out.
  So, he desperately needs a system, and then when he entered 1,234,567,009 time, will give the corresponding read the law:
  1,234,567,009
  represented as Pinyin
  shi er yi san qian si bai wu shi liu wan qi qian ling jiu
  so he only needs to read shining on it.
  Your task is to help him design a system like this: Given a string of digits, you help him into reading and writing Chinese Pinyin string accordance with the norms of the adjacent two syllables with a lattice open spaces.
  Note must be in strict accordance with specifications, such as "10010" is read as "yi wan ling yi shi" instead of "yi wan ling shi", " 100000" is read as "shi wan" instead of "yi shi wan", "2000 " read as "er qian" instead of "liang qian".
Input format
  has a sequence of digits, the numerical size of not more than 2,000,000,000.
Output format
  is a string of lower case letters, spaces and commas composition, represents the number of English reading.
Sample Input
1234567009
sample output
shi er yi san qian si bai wu shi liu wan qi qian ling jiu

Code:

import java.util.Scanner;

public class BASIC020数的读法 {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		String[] shuzhi = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
		String[] danwei = { "", "", "shi", "bai", "qian", "wan", "shi", "bai", "qian", "yi", "shi" };
		String str = s.next();
		s.close();
		int n = str.length();
		for (int i = 0; i < n; i++) {
			int j = str.charAt(i) - '0';
			if (j == 0) {
				if ((n - i) == 5 || (n - i) == 9){
					System.out.printf("%s ", danwei[n - i]);
					}
				if ((n - i) != 1 && str.charAt(i + 1) != '0'){
					System.out.printf("%s ", "ling");
					}
			} else if ((n == 2 || n == 6 || n == 10) && j == 1 && i == 0){
				System.out.printf("%s ", "shi");
				}
			else{
				System.out.printf("%s %s ", shuzhi[j], danwei[n - i]);
			}
		}
	}
}
Published 29 original articles · won praise 1 · views 1082

Guess you like

Origin blog.csdn.net/DAurora/article/details/105328133