华为笔试 数字转中文拼音
2016-09-07 10:21
274 查看
题目:输入一个整数数字,输出该数字的中文拼音
比如:54 wushisi
100 yibai
6006 liuqianlingliu
60006 liuwanlingliu
解题思路:
首先要把数字的读音和数字对应起来,十位、百位、千位。。。等等的读音对应起来
接下来要考虑到几种情况:
1、各位为零
2、中间有连续几个零
3、当整数大于万以后怎么读
参考代码:
比如:54 wushisi
100 yibai
6006 liuqianlingliu
60006 liuwanlingliu
解题思路:
首先要把数字的读音和数字对应起来,十位、百位、千位。。。等等的读音对应起来
接下来要考虑到几种情况:
1、各位为零
2、中间有连续几个零
3、当整数大于万以后怎么读
参考代码:
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String line = in.nextLine(); HashMap<Integer, String> frist = new HashMap<Integer, String>(); frist.put(1, "shi"); frist.put(2, "bai"); frist.put(3, "qian"); frist.put(4, "wan"); frist.put(8, "yi"); HashMap<Integer, String> read = new HashMap<Integer, String>(); read.put(1, "yi"); read.put(2, "er"); read.put(3, "san"); read.put(4, "si"); read.put(5, "wu"); read.put(6, "liu"); read.put(7, "qi"); read.put(8, "ba"); read.put(9, "jiu"); read.put(0, "ling"); int num = Integer.parseInt(line); StringBuilder sb = new StringBuilder(); int i = 0; int j = 0; while (num != 0) { j = num % 10; if (i != 0 && j != 0) { sb.insert(0, frist.get(i)); sb.insert(0, read.get(j)); } else if (i != 0 && j == 0) { if (sb.toString().length() != 0 && sb.indexOf("ling")!=0){//中间连续几个零 sb.insert(0, read.get(0)); } } else if (i == 0 && j != 0) { sb.insert(0, read.get(j)); } i++; if (i > 4) { i = i % 4;//当位数大于万 } num = num / 10; } String str = sb.toString(); str = str.replaceAll("null", ""); System.out.println(str); } } }
相关文章推荐
- 华为机试:中文整数读法的拼音转数字
- 华为2017年8月30日校招编程真题01-数字的中文拼音和英文单词之间互相转换
- 输入阿拉伯数字(整数),输出对应的中文(美团网2014年9月16日笔试题目之一)
- 求最长数字串(华为勇敢星笔试第一题)
- 将10进制数字转化为26进制用A-Z来表示(华为笔试题第二题)
- 华为机试—拼音翻译成阿拉伯数字(有Wan,Qian,Bai,Shi单位)
- 月经型笔试题:“数字金额如何转换为中文大写?”思路清晰版写法
- 华为机试-拼音翻译成阿拉伯数字
- 拼音翻译成阿拉伯数字_华为SO挑战赛2015年8月
- 2016.3.18华为上机题:拼音转数字
- 华为2013校园招聘上机笔试题-数字转化为字符串,并判断有无重复字符串
- 删数字(华为笔试题)
- 华为机试—拼音翻译成阿拉伯数字(只有数字拼音)
- [算法]华为笔试题——拼音与英文转换
- qunar网2014年9月16日笔试题编程题3(中文大写数字转换成阿拉伯数字)题解
- 华为机试题之拼音转数字
- 2015年华为校招机试题和代码实现(分解字符串,拼音转数字,去除重复字符并排序,等式变换)
- java笔试题之----将阿拉伯数字转换成中文
- 华为笔试题目,打印n个数字中最小的m个
- 输入阿拉伯数字(整数),输出相应的中文(美团网2014年9月16日笔试题目之中的一个)