蓝桥杯---数的读法

问题描述:
     他迫切地需要一个系统,然后当他输入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*/

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		//输入字符串
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		String result="";
		if(n<10000){
			result=thousandSay(n);
		}else if(n<100000000){
			int a=n%10000;
			int b =n/10000;
			if(a==0){
				result=thousandSay(b)+" wan";
			}else{
				if(a<1000){
					result=thousandSay(b)+" wan ling "+thousandSay(a);
				}else{
					result=thousandSay(b)+" wan "+thousandSay(a);
				}
					
			}
		}else{
			int a =n%10000;
			int b=(n/10000)%10000;
			int c= n/100000000;
			if(a==0&&b==0){
				result=thousandSay(c)+" yi";
			}else if(a==0&&b!=0){
				if(b<1000){
					result=thousandSay(c)+" yi ling "+thousandSay(b)+" wan";
				}else{
					result=thousandSay(c)+" yi "+thousandSay(b)+" wan";
				}
			}else if(a!=0&&b==0){
				result=thousandSay(c)+" yi ling "+thousandSay(a);
			}else{
				if(b<1000&&a<1000){
					result=thousandSay(c)+" yi ling "+thousandSay(b)+" wan ling "+thousandSay(a);
				}else if(b<1000&&a>1000){
					result=thousandSay(c)+" yi ling "+thousandSay(b)+" wan "+thousandSay(a);
				}else if(b>1000&a<1000){
					result=thousandSay(c)+" yi "+thousandSay(b)+" wan ling "+thousandSay(a);
				}else{
					result=thousandSay(c)+" yi "+thousandSay(b)+" wan "+thousandSay(a);
				}
			}
		}
		
		System.out.println(result);

		
	}

	
	public static String thousandSay(int n){
		String result="";
		String[] arr=new String[]{"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
		if(n<10){
			result=arr[n];
		}else if(n<100){
			int a = n%10;
			int b = n/10;
			if(b==1){
				result="shi "+arr[a];
			}else{
				result=arr[b]+" shi "+arr[a];
			}
		}else if(n<1000){
			int a=n%10;//个位
			int b=(n/10)%10;//十位
			int c=n/100;//百位
			//x0x
			if(a!=0&&b==0){
				result=arr[c]+" bai ling "+arr[a];
			}
			//xx0
			else if(a==0&&b!=0){
				result=arr[c]+" bai "+arr[b]+" shi";
			}
			//xxx
			else if(a!=0&&b!=0){
				result=arr[c]+" bai "+arr[b]+" shi "+arr[a];
			}
			//x00
			else{
				result=arr[c]+" bai";
			}
		}else{
			int a = n%10;//个位
			int b = (n/10)%10;//十位
			int c = (n/100)%10;//百位
			int d = n/1000;//千位
			//x000
			if(a==0&&b==0&&c==0){
				result=arr[d]+" qian";
			}
			//x00x
			else if(a!=0&&b==0&&c==0){
				result=arr[d]+" qian ling "+arr[a];
			}
			//x0x0
			else if(a==0&&b!=0&&c==0){
				result=arr[d]+" qian ling "+arr[b]+" shi";
			}
			//x0xx
			else if(a!=0&&b!=0&&c==0){
				result=arr[d]+" qian ling "+arr[b]+" shi "+arr[a];
			}
			//xx00
			else if(a==0&&b==0&&c!=0){
				result=arr[d]+" qian "+arr[c]+" bai";
			}
			//xx0x
			else if(a!=0&&b==0&&c!=0){
				result=arr[d]+" qian "+arr[c]+" bai ling "+arr[a];
			}
			//xxx0
			else if(a!=0&&b!=0&&c==0){
				result=arr[d]+" qian "+arr[c]+" bai "+arr[b]+" shi";
			}
			//xxxx
			else if(a!=0&&b!=0&&c!=0){
				result=arr[d]+" qian "+arr[c]+" bai "+arr[b]+" shi "+arr[a];
			}
		}
		return result;
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/qq_39487875/article/details/86656786