数的读法-题解

来源:互联网 发布:linux下如何安装mysql 编辑:程序博客网 时间:2024/04/30 15:24
基础练习 数的读法  
时间限制: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
解题思路: 将输入的字符串转化成10位的字符串,对其进行统一的处理。 例如: 
Sample Input:  7009   ------  nnnnnn7009
Sample Input:  63635858 ------ nn63635858
Sample Input:  1234567009 ------- 1234567009
 这样将接收到的字符串,变成10位的字符串,n表示不存在该位,转换成拼音, 最后过滤即可。
源码(JAVA):
import java.util.Scanner;


/**
 * 
 * 
 *   12 3456 7009
 *   20 0000 0000
 */
public class Asist
{


public static String nums1[] = {"ling", "yi", "er", "san", "si", "wu","liu","qi","ba","jiu","shi"};
public static String nums2[] = {"shi","bai","qian","wan","yi"};
// 20 0000 0000 , 21 0000 0000, 20 0010 0001
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
        
while(str.length() < 10)
{
str = "n"+str;
}

String re = fun(str);
System.out.println(check(re));
}

// 12 3456 7009
private static String fun(String str)
{
String re = "";
int ch;

// 1     12 3456 7009
if (str.charAt(0) != 'n')
{
ch = str.charAt(0) - '0';
re += nums1[ch] + " " + nums2[0]+" ";
}
    // 2     12 3456 7009
if (str.charAt(1) != 'n')
{
ch = str.charAt(1) - '0';
if (ch == 0) re += nums2[4] + " ";
else
re += nums1[ch] + " "+nums2[4]+" ";
}
    // 3     12 3456 7009
if (str.charAt(2) != 'n')
{
ch = str.charAt(2) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[2]+" ";
}
    // 4     12 3456 7009
if (str.charAt(3) != 'n')
{
ch = str.charAt(3) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[1]+" ";
}
    // 5     12 3456 7009
if (str.charAt(4) != 'n')
{
ch = str.charAt(4) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[0]+" ";
}
    // 6     12 3456 7009
if (str.charAt(5) != 'n')
{
ch = str.charAt(5) - '0';
if (ch == 0) re += nums2[3] +" ";
else
re += nums1[ch] + " "+nums2[3]+" ";
}
    // 7     12 3456 7009
if (str.charAt(6) != 'n')
{
ch = str.charAt(6) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[2]+" ";
}
    // 8     12 3456 7009
if (str.charAt(7) != 'n')
{
ch = str.charAt(7) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[1]+" ";
}
    // 9     12 3456 7009
if (str.charAt(8) != 'n')
{
ch = str.charAt(8) - '0';
if (ch == 0) re += nums1[ch] +" ";
else
re += nums1[ch] + " "+nums2[0]+" ";
}
    // 10     12 3456 7009
if (str.charAt(9) != 'n')
{
ch = str.charAt(9)-'0';
re += nums1[ch];
}
return re;
}

public static String check(String re)
{
// 处理末尾 'ling'
    while (re.trim().endsWith("ling"))
    {
    re = re.substring(0, re.trim().length()-4);
    }
    // 处理连续的'ling'
    while (re.trim().indexOf("ling ling")!=-1)
    {
    re = re.replaceAll("ling ling", "ling");
    }
    // 处理'ling wan'
    if (re.trim().indexOf("ling wan")!=-1)
    {
    re = re.replaceAll("ling wan", "wan");
    }
    // 处理 'yi shi er wan' 去掉 'yi'
    if (re.trim().startsWith("yi shi"))
    {
    re = re.substring(3, re.length());
    }
    return re;
}
}
0 0
原创粉丝点击