问题描述
当输入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”。
样例输入
1234567009
样例输出
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
这道题自我感觉就是一个时间的问题,不是很难,但可能很费时间package 蓝桥杯VIP;
import java.util.Arrays;
import java.util.Scanner;
public class 数的读法 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
readToChinese(scanner.nextInt());
}
public static void readToChinese(int n){
if(n < 10000){
thousandSay(n);
System.out.println();
}else if(n < 100000000){
int n1 = n / 10000;
int n2 = n % 10000;
if(n2 == 0){
thousandSay(n1);
System.out.print("wan");
System.out.println();
}else {
thousandSay(n1);
System.out.print("wan ");
thousandSay(n2);
System.out.println();
}
}else if(n < 2000000000){
int n1 = n / 100000000;
int n2 = (n/10000) % 10000;
int n3 = n % 10000;
thousandSay(n1);
System.out.print("yi ");
thousandSay(n2);
System.out.print("wan ");
thousandSay(n3);
System.out.println();
}else if(n == 2000000000){
System.out.println("er yi");
System.out.println();
}else {
System.out.println("输入错误,正确范围是(0-20 0000 0000)");
}
}
public static void thousandSay(int n){
String[] arr = new String[]{"ling ","yi ","er ","san ","si ","wu ","liu ","qi ","ba ","jiu "};
String shi = "shi ";
String bai = "bai ";
String qian = "qian ";
int num = n;
if(n < 10){
System.out.print(arr[n]);
}else if(n < 100){
int a = n / 10;
int b = n % 10;
if(b == 0){
System.out.print(arr[a]+shi);
}else if(a == 1){
System.out.print(shi+arr[b]);
} else {
System.out.print(arr[a]+shi+arr[b]);
}
}
else if(n < 1000){
int a = n / 100;
int b = (n / 10) % 10;
int c = n % 10;
if(b == 0 && c == 0){
System.out.print(arr[a]+bai);
}else if(b == 0 & c != 0){
System.out.print(arr[a]+bai+arr[0]+arr[c]);
}else if(b != 0 && c == 0){
System.out.print(arr[a]+bai+arr[b]+shi);
}else {
System.out.print(arr[a]+bai+arr[b]+shi+arr[c]);
}
}else if(n < 10000){
int a = n / 1000; //表示千位
int b = (n / 100) % 10; //表示百位
int c = (n / 10) % 10; //表示十位
int d = n % 10; //表示个位
if(b == c && c == d && d == 0){
System.out.print(arr[a]+qian);
}else if (b == 0 && c != 0 & d != 0){
System.out.print(arr[a]+qian+arr[0]+arr[c]+shi+arr[d]);
}else if(b == 0 && c == 0 && d != 0){
System.out.print(arr[a]+qian+arr[0]+arr[d]);
}else if (b == 0 && c != 0 && d == 0){
System.out.print(arr[a]+qian+arr[0]+arr[c]+shi);
}else if(c == 0 && d == 0){
System.out.print(arr[a]+qian+arr[b]+bai);
}else if (c == 0 && d != 0){
System.out.print(arr[a]+qian+arr[b]+bai+arr[0]+arr[d]);
}else if(d == 0){
System.out.print(arr[a]+qian+arr[b]+bai+arr[c]+shi);
}else {
System.out.print(arr[a]+qian+arr[b]+bai+arr[c]+shi+arr[d]);
}
}
}
}