How to read the basic exercises of the Blue Bridge Cup questions BASIC-20 JAVA

How to read the basic exercises of test questions

Resource limit
Time limit: 1.0s Memory limit: 512.0MB

Problem description
  Professor Tom is teaching a course on genes to graduate students, and one thing makes him quite headache: there are thousands of base pairs on a chromosome, they are numbered from 0 to millions, thousands Ten million, even hundreds of millions.
  For example, when explaining the base at position 1234567009 to students, it is difficult to read the numbers accurately.
  Therefore, he desperately needs a system, and then when he enters 12 3456 7009, he will give the corresponding way of   saying :
  1.2343 billion 609,700 is
expressed in Chinese pinyin as
  shi er yi san qian si bai wu shi liu wan qi qian ling jiu
  so that he only needs to follow the instructions.
  Your task is to help him design such a system: given a string of Arabic numerals, you help him to convert it into a Chinese pinyin string according to the Chinese reading and writing standards, and the two adjacent syllables are separated by a space character.
  Note that you must strictly follow the specifications, for example, "10010" is pronounced "yi wan ling yi shi" instead of "yi wan ling shi", "100000" is pronounced "shi wan" instead of "yi shi wan", and "2000" is read Make "er qian" instead of "liang qian".

The input format
  has a number string with a numeric value not exceeding 2,000,000,000.

Output format
  is a string composed of lowercase English letters, commas, and spaces, which means the English reading of the number.

Sample input
1234567009

Sample output
shi er yi san qian si bai wu shi liu wan qi qian ling jiu

Main points

  1. There are a lot of details on this question, and it must be very serious. Your code may pass the evaluation of the Blue Bridge Cup and get full marks, but it does not mean that your question is correct. You can check the following input and output.
    • 100000 shi wan
    • 100010 shi wan ling yi shi
    • 10010000 yi qian ling yi wan
  2. |||Similarities and differences
    • The same point: ||and |can be used as the logical OR operator, which means logical or (or), as long as one side is true, the result is true. Otherwise, when the result of the expression on both sides of the operator is false, the entire operation result is false.
    • Difference: The difference between the two is that the former will have a "short circuit" in the process of execution. That is, if ”||”the expression in front of the symbol is true, then the following expression will not be executed on the source line, and returns true directly.
    • Sample code:
int i = 0, j = 1;
        if (i == 0 || ++j > 0) {
            System.out.println("j=" + j); //输出j=1,即未执行 ++j > 0语句
        }
        if (i == 232 || ++j > 0) {
            System.out.println("j=" + j); //输出j=2,执行++j > 0该语句
}

The symbol "&&" and the symbol "&" are the same.
Understand these, you can understand why the second piece of code below deletes some judgment expressions

 boolean b = (i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] != '0')
		|| (i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 3] != '0')
		|| (i < len1 - 3 && charArray[i + 1] == '0');
//因为||的短路特色,有的判断条件就可以删除
b = (((i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] != '0')
		|| (i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] == '0' && charArray[i + 3] != '0')
		|| i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] == '0' && charArray[i + 3] == '0'));

Code for this question

import java.util.Scanner;

public class ReadingOfNum4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String numStr = sc.next();
        sc.close();
        String[] numPinYin = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
        String[] digitPinYin = {"", "", "shi", "bai", "qian", "wan", "shi", "bai", "qian", "yi", "shi"};
        String[] digitPinYin2 = {"", "", "shi", "bai", "qian", "wan", "shi wan", "bai wan", "qian wan", "yi", "shi yi"};
        char[] charArray = numStr.toCharArray();
        int len1 = charArray.length;
        for (int i = 0; i < len1; i++) {
            int j = numStr.charAt(i) - '0';
            if (j == 0) {
                if (i < len1 - 1 && charArray[i + 1] != '0') {
                    System.out.print("ling" + " "); //两个非0数字中间的许多0的最后一个0读“ling”,比如10001那三个0的最后一个读“ling”
                }
            } else {
                /*boolean b = (i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] != '0')
                        || (i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 3] != '0')
                        || (i < len1 - 3 && charArray[i + 1] == '0');
                //因为||的短路特色,有的判断条件就可以删除
                b = (((i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] != '0')
                        || (i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] == '0' && charArray[i + 3] != '0')
                        || i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] == '0' && charArray[i + 3] == '0')
                );*/
                boolean b = i < len1 - 3 && charArray[i + 1] == '0' && charArray[i + 2] == '0' && charArray[i + 3] == '0';
                if ((len1 - i == 2 || len1 - i == 6 || len1 - i == 10) && j == 1 && i == 0) {//关于shi 100000读shi wan而不是yi shi wan
                    if (b) {
                        System.out.print(digitPinYin2[len1 - i] + " ");
                    } else {
                        System.out.print(digitPinYin[len1 - i] + " ");
                    }
                } else {
                    if (b) {
                        System.out.print(numPinYin[j] + " " + digitPinYin2[len1 - i] + " ");
                    } else {
                        System.out.print(numPinYin[j] + " " + digitPinYin[len1 - i] + " ");
                    }
                }
            }
        }
    }
}

reference

Gangster Blog "How to read basic exercises"

Published 113 original articles · Liked 105 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/weixin_43124279/article/details/105416892