* * * BASIC-20 数的读法

资源限制

时间限制:1.0s   内存限制:512.0MB

问题描述

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”。

输入格式

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

输出格式

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

样例输入

1234567009

样例输出

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

拓展知识点:

1、replace方法
    该方法的作用是替换字符串中所有指定的字符,然后生成一个新的字符串。经过该方法调用以后,原来的字符串不发生改变。
2、endsWith(String suffix)    suffix -- 代表指定的后缀。
    这个方法用于测试字符串是否以指定的后缀结束;如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。
3、startsWith() 方法用于检测字符串是否以指定的子字符串开始。
    如果是以指定的子字符串开头返回 true,否则 false。startsWith() 方法对大小写敏感。
4、Math.ceil(double a) 返回最小的(最接近负无穷大)double值,大于或相等于参数,并相等于一个整数。  https://blog.csdn.net/Boiling_cola/article/details/75098304

Code

Java源代码:

import java.util.Scanner;

public class Main {
	static String[] du = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };

	static String deal(String s) { // 处理非法读法
        s = s.replace("ling yi", "yi");
		s = s.replace("ling wan", "wan");
		s = s.replace("ling bai", "ling");
		s = s.replace("ling shi", "ling");
		s = s.replace("ling ling", "ling");
		if (s.endsWith(" ling")) // 末尾的零不读
			s = s.substring(0, s.length() - 5);
		if (s.startsWith("yi shi")) // 开头的10不读做“一十”而读作“十”
			s = s.replace("yi shi", "shi");
		if (s.endsWith("  wan ")) // 避免出现1,0000,0000被读成“yi yi wan ”;
			s = s.replace("  wan ", "");
		return s;
	}

	static String[] split(String s) { // 将一串数字从后往前分为若干个四位数,如123456789会被分成:1 2345 6789作为三个元素(1放在2号下标位置,2345放在1号下标位置,6789放在0号下标位置)存入数组
		int t = (int) Math.ceil((double) s.length() / 4); // 4位数字占数组的一个下标
		String[] res = new String[t]; // t代表需要多少空间
		for (int i = t - 1; i > 0; i--) {
			res[i] = s.substring(s.length() - 4, s.length()); // 从后边开始每四位数字存一个下标,最后的4位存在0号下标处,0号下标存千到个位,1号下标存从后边数第二个4位(万到千万位)......
			s = s.substring(0, s.length() - 4); // 对未存入(除了本次刚存进去的后四位)的数位进行下一次存储
		}
		res[0] = s;
		return res;
	}

	static String f1(String s) { // 把一个四位数读出来,本来是原来每4位数放在一个下标里,在读的时候再把一个下标中的4位数作为一个数组,对这4位数用一次f1函数:(这4位数中高位在0号下标)
		String fristZero = "";
		while (s.startsWith("0")) { // 如果最开头是零
			s = s.substring(1, s.length());
			fristZero = "ling ";
		}
		String res = "";
		if (s.length() == 1)
			res = fristZero + du[s.charAt(0) - 48];
		else if (s.length() == 2)
			res = fristZero + du[s.charAt(0) - 48] + " shi " + du[s.charAt(1) - 48];
		else if (s.length() == 3)
			res = fristZero + du[s.charAt(0) - 48] + " bai " + du[s.charAt(1) - 48] + " shi " + du[s.charAt(2) - 48];
		else if (s.length() == 4)
			res = fristZero + du[s.charAt(0) - 48] + " qian " + du[s.charAt(1) - 48] + " bai " + du[s.charAt(2) - 48]
					+ " shi " + du[s.charAt(3) - 48];
		return deal(res); // 这里每4位表示成读法形式后,一定要调用一次deal来出去这4位读法形式中的不正确读法
	}

	static String f2(String[] s) { // 把一串数字读出来
		if (s.length == 1)
			return f1(s[0]); // s[0]中有4位数字,s[1]中有4位数字......
		else if (s.length == 2)
			return f1(s[0]) + " wan " + f1(s[1]);
		else if (s.length == 3)
			return f1(s[0]) + " yi " + f1(s[1]) + " wan " + f1(s[2]);
		return "error!";
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		String res = f2(split(s));
		System.out.println(deal(res)); // 最后把非法的读法都处理掉
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值