基础练习 数的读法(ACMORE1574)

基础练习 数的读法(ACMORE1574)

题目

Description

Tom教授正在给研究生讲授一门关于基因的课程,有一件事情让他颇为头疼:一条染色体上有成千上万个碱基对,它们从0开始编号,到几百万,几千万,甚至上亿。比如说,在对学生讲解第1234567009号位置上的碱基时,光看着数字是很难准确的念出来的。
所以,他迫切地需要一个系统,然后当他输入12 3456 7009时,会给出相应的念法:
十二亿三千四百五十六万七千零九
用汉语拼音表示为
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
这样他只需要照着念就可以了。
你的任务是帮他设计这样一个系统:给定一个阿拉伯数字串,你帮他按照中文读写的规范转为汉语拼音字串,相邻的两个音节用一个空格符格开。
注意必须严格按照规范,比如说“10010”读作“yi wan ling yi shi”而不是“yi wan ling shi”,“100000”读作“shi wan”而不是“yi shi wan”,“2000”读作“er qian”而不是“liang qian”。

Input

有一个数字串,数值大小不超过2,000,000,000。

Output

是一个由小写英文字母,逗号和空格组成的字符串,表示该数的英文读法。

Sample Input

1234567009

Sample Output

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

Source
蓝桥杯
题目链接: https://acmore.cc/problem/LOCAL/1574

简析

题意很简单,耐心分析规律即可。主要有三点:
1.可以发现将每四位分成一组,中间用亿、万连接起来比较简便。
2.将分好的四位一组的数每一位分别判断一下
3.十位为1时根据百位的情况有shi、yi shi两种可能

注:偶然发现后台数据不够全面,有一个类似下面这样的数据没有测到:

30002000

不确定的读音可以去这个在线网站查一下:
http://www.nicetool.net/embed/number_to_chinese.html
数的读法

AC的代码

Java版(不算简洁,但是用了逆序和用"null"补位,较好理解)

import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String[] npin = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
        String[] wpin = {"null", "wan", "yi"};
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        StringBuilder nStr = new StringBuilder(scanner.nextLine());
        nStr = nStr.reverse();
        String[] res = new String[27];
        for(int i = 0; i < 27; i++)
            res[i] = "null";

        int ires = 0;
        for(int i = 0; i < nStr.length(); i+=4) {
            res[ires++] = wpin[i/4];

            //个位
            if(!(nStr.charAt(i) == '0' && nStr.charAt(i+1) != '!'))
                res[ires++] = npin[nStr.charAt(i)-'0'];
            else
                res[ires++] = "null";

            //十位
            if(i+1 >= nStr.length())
                break;
            else if(nStr.charAt(i+1) == '1' && (i+2 >= nStr.length() || i+2 < nStr.length() && nStr.charAt(i+2) != '0')) {
                res[ires++] = "shi";
                res[ires++] = "null";
            }
            else if(nStr.charAt(i+1) == '0') {
                res[ires++] = "ling";
                res[ires++] = "null";
            }
            else {
                res[ires++] = "shi";
                res[ires++] = npin[nStr.charAt(i+1)-'0'];
            }

            //百位
            if(i+2 >= nStr.length())
                break;
            else if(nStr.charAt(i+2) == '0') {
                res[ires++] = "ling";
                res[ires++] = "null";
            }
            else {
                res[ires++] = "bai";
                res[ires++] = npin[nStr.charAt(i+2)-'0'];
            }

            //千位
            if(i+3 >= nStr.length())
                break;
            else if(nStr.charAt(i+3) == '0') {
                res[ires++] = "ling";
                res[ires++] = "null";
            }
            else {
                res[ires++] = "qian";
                res[ires++] = npin[nStr.charAt(i+3)-'0'];
            }
        }
        String pre = "null";
        for(int i = 0; i < 27; i+=9)
            for(int j = i; j < i + 9; j ++)
                if(res[j].equals("ling") || res[j].equals("null"))
                    res[j] = "null";
                else
                    break;
        for(int i = 23; i >= 0; i--)
            if(!res[i].equals("null")) {
                if(pre.equals("ling") && res[i].equals("ling")) {
                    continue;
                }
                else {
                    System.out.print(res[i] + " ");
                    pre = res[i];
                }
            }
    }
}
发布了118 篇原创文章 · 获赞 479 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/y_universe/article/details/86751312