蓝桥杯 数的读法

蓝桥杯 数的读法

题目

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。

输出格式

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

输入样例

1
1234567009

输出样例

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

思路

采用了分治的思想,先划分为[0,10000],[10000,100000000],[100000000,+][0, 10000], [10000, 100000000], [100000000, +∞],1万以上的,把数字拆成几部分,分别处理。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.Scanner;
public class Main {
public static String[] a = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
public static String[] b = {"yi","wan","qian","bai","shi"};

public static void smallRead(char[] num) {
if (num.length==2&&num[0]=='1') {
System.out.print("shi");
if(num[1]!='0'){
System.out.print(" "+a[num[1]-48]);
}
return;
}
for(int i = 0;i<num.length;i++){
if(num[i] == '0'){
for(int j = i;j<num.length;j++){
if(num[j]!='0'){
System.out.print(" ling ");
char[] aa = new char[num.length-j];
System.arraycopy(num, j, aa, 0, num.length-j);
readNum(aa);
break;
}//如果等于0 则不输出
}
break;
}else {
if(i!=0){
System.out.print(" ");
}
if(i==num.length-1){
System.out.print(a[num[i]-48]);
}else {
System.out.print(a[num[i]-48]+" "+b[6 - num.length + i]);
}
}
}
}

public static Boolean isZero(char[] a){
Boolean temp = true;
for(char c:a){
if(c != 48){
temp = false;
}
}
return temp;
}
public static void readNum(char[] num) {
if(num.length<=5){
//10000以下
smallRead(num);
}else if(num.length<9){
//一亿以下,一万以上
char[] yi = new char[num.length - 4];
char[] wang = new char[4];
System.arraycopy(num, num.length - 4, wang, 0, 4);
System.arraycopy(num, 0, yi, 0, num.length - 4);
smallRead(yi);
//判断后面是不是全为0
if(isZero(wang)){
System.out.print(" wan");
return;
}else {
System.out.print(" wan ");
smallRead(wang);
}

}else if(num.length>=9){
//一亿以上,两亿以下
char[] wang = new char[4];
char[] yi = new char[4];
char[] max = new char[num.length - 8];
System.arraycopy(num, num.length - 4, wang, 0, 4);
System.arraycopy(num, num.length - 8, yi, 0, 4);
System.arraycopy(num, 0, max, 0, num.length - 8);
smallRead(max);
//判断后面是不是全为0
if(isZero(yi)){
System.out.print(" yi");
return;
}else {
System.out.print(" yi ");
smallRead(yi);
if(isZero(yi)){
System.out.print(" wan");
return;
}else {
System.out.print(" wan ");
smallRead(wang);
}
}
}
}


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[] num = scanner.nextLine().toCharArray();
scanner.close();
readNum(num);
}
}
文章作者: Shengyaqingfeng
文章链接: https://creazyboyone.github.io/lqb61/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Shengyaqingfeng's Blog