zoukankan      html  css  js  c++  java
  • 基础练习 数的读法

      基础练习 数的读法  
    时间限制: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
    import java.util.Scanner;  
      
    public class Main {  
      
        static String[] unit = { "", "", "wan ", "yi " };  
      
        static String[] digit = { "", "", "shi ", "bai ", "qian " };  
      
        public static void main(String[] args) {  
            Scanner scanner = new Scanner(System.in);  
      
            while (scanner.hasNext()) {  
                String str = scanner.nextLine();  
                char[] chs = str.toCharArray();  
      
                int len = chs.length;  
                int index = 0;
      
                int group = (len - 1) / 4 + 1;
                int rest = len % 4;
      
                String spell;  
                StringBuffer result = new StringBuffer();  
      
                if (rest != 0) {  
                    if (rest == 2) {  
                        if (chs[index] == '1') {  
                            result.append("shi ");  
                        } else {  
                            spell = getSpelling(chs[index]);  
                            result.append(spell + "shi ");  
                        }  
                        index++;  
                          
                        if (chs[index] != '0') {  
                            result.append(getSpelling(chs[index]));  
                        }  
                        index++;  
      
                    } else {  
                        boolean flag = true;  
                        for (int i = rest; i > 0; i--) {  
                            char ch = chs[index++];  
                            if (ch != '0') {  
                                spell = getSpelling(ch);  
                                if (flag) {  
                                    result.append(spell + digit[i]);  
                                } else {  
                                    result.append("ling " + spell + digit[i]);  
                                }  
                            } else {  
                                flag = false;  
                            }  
                        }  
                    }  
                    result.append(unit[group--]);  
                }  
      
                for (int i = group; i > 0; i--) {  
                    boolean flag = true;  
                    for (int j = 4; j > 0; j--) {  
                        char ch = chs[index++];  
                        if (ch != '0') {  
                            spell = getSpelling(ch);  
                            if (flag) {  
                                result.append(spell + digit[j]);  
                            } else {  
                                result.append("ling " + spell + digit[j]);  
                                flag = true;  
                            }  
                        } else {  
                            flag = false;  
                        }  
                    }  
                    result.append(unit[group--]);  
                }  
      
                result.deleteCharAt(result.length() - 1);  
                System.out.println(result);  
            }  
        }  
      
        private static String getSpelling(char c) {  
            String spell = null;  
            switch (c) {  
            case '0':  
                spell = "ling ";  
                break;  
            case '1':  
                spell = "yi ";  
                break;  
            case '2':  
                spell = "er ";  
                break;  
            case '3':  
                spell = "san ";  
                break;  
            case '4':  
                spell = "si ";  
                break;  
            case '5':  
                spell = "wu ";  
                break;  
            case '6':  
                spell = "liu ";  
                break;  
            case '7':  
                spell = "qi ";  
                break;  
            case '8':  
                spell = "ba ";  
                break;  
            case '9':  
                spell = "jiu ";  
                break;  
            }  
            return spell;  
        }  
    }  
     
  • 相关阅读:
    条件编译中的基本语法
    UITableView中headerView视察滚动的简单实现
    CocoaPods使用简单回顾
    CocoaPods第三方类库管理工具的简单使用
    Xcode中release和debug模式
    转:关于LazyTableImage
    汉字与UTF-8编码之间的转换
    结构体与字符串之间的转换
    MFC中小笔记(二)
    升级 WIN8.1 VC6.0和 Visual Assist 的使用问题
  • 原文地址:https://www.cnblogs.com/watchfree/p/5313622.html
Copyright © 2011-2022 走看看