zoukankan      html  css  js  c++  java
  • 华为笔试 数字转中文拼音

    题目:输入一个整数数字,输出该数字的中文拼音

    比如: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);
            }
        }
    }
  • 相关阅读:
    算法(5)
    字典
    算法(4)
    AD域设置
    css两句话搞定漂亮表格样式
    Dev控件用法 aspxTreeList 无刷新 aspxGridView 数据
    ASP.Net 验证视图状态 MAC 失败
    C# 客服端上传文件与服务器器端接收 (简单代码)
    Linq to SQL 类型的对象图包含循环,如果禁用引用跟踪,择无法对其进行序列化。
    C# 导出 Excel 数字列出现‘0’的解决办法
  • 原文地址:https://www.cnblogs.com/googlemeoften/p/5848414.html
Copyright © 2011-2022 走看看