zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯 蓝桥杯VIP 基础练习 数的读法

    问题描述

    当输入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”。

    样例输入

    1234567009

    样例输出

    shi er yi san qian si bai wu shi liu wan qi qian ling jiu

    这道题自我感觉就是一个时间的问题,不是很难,但可能很费时间
    package 蓝桥杯VIP;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class 数的读法 {
    	  public static void main(String[] args){
    	        Scanner scanner = new Scanner(System.in);
    	        readToChinese(scanner.nextInt());
    	    }
    
    
    public static void readToChinese(int n){
            if(n < 10000){
                thousandSay(n);
                System.out.println();
            }else if(n < 100000000){
                int n1 = n / 10000;
                int n2 = n % 10000;
                if(n2 == 0){
                    thousandSay(n1);
                    System.out.print("wan");
                    System.out.println();
                }else {
                    thousandSay(n1);
                    System.out.print("wan ");
                    thousandSay(n2);
                    System.out.println();
                }
            }else if(n < 2000000000){
                int n1 = n / 100000000;
                int n2 = (n/10000) % 10000;
                int n3 = n % 10000;
                thousandSay(n1);
                System.out.print("yi ");
                thousandSay(n2);
                System.out.print("wan ");
                thousandSay(n3);
                System.out.println();
            }else if(n == 2000000000){
                System.out.println("er yi");
                System.out.println();
            }else {
                System.out.println("输入错误,正确范围是(0-20 0000 0000)");
            }
        }
    public static void thousandSay(int n){
        String[] arr = new String[]{"ling ","yi ","er ","san ","si ","wu ","liu ","qi ","ba ","jiu "};
        String shi = "shi ";
        String bai = "bai ";
        String qian = "qian ";
        int num = n;
        if(n < 10){
            System.out.print(arr[n]);
        }else if(n < 100){
            int a = n / 10;
            int b = n % 10;
            if(b == 0){
                System.out.print(arr[a]+shi);
            }else if(a == 1){
                System.out.print(shi+arr[b]);
            } else {
                System.out.print(arr[a]+shi+arr[b]);
            }
        }
        else if(n < 1000){
            int a = n / 100;
            int b = (n / 10) % 10;
            int c = n % 10;
            if(b == 0 && c == 0){
                System.out.print(arr[a]+bai);
            }else if(b == 0 & c != 0){
                System.out.print(arr[a]+bai+arr[0]+arr[c]);
            }else if(b != 0 && c == 0){
                System.out.print(arr[a]+bai+arr[b]+shi);
            }else {
                System.out.print(arr[a]+bai+arr[b]+shi+arr[c]);
            }
        }else if(n < 10000){
            int a = n / 1000; //表示千位
            int b = (n / 100) % 10; //表示百位
            int c = (n / 10) % 10; //表示十位
            int d = n % 10; //表示个位
            if(b == c && c == d && d == 0){
                System.out.print(arr[a]+qian);
            }else if (b == 0 && c != 0 & d != 0){
                System.out.print(arr[a]+qian+arr[0]+arr[c]+shi+arr[d]);
            }else if(b == 0 && c == 0 && d != 0){
                System.out.print(arr[a]+qian+arr[0]+arr[d]);
            }else if (b == 0 && c != 0 && d == 0){
                System.out.print(arr[a]+qian+arr[0]+arr[c]+shi);
            }else if(c == 0 && d == 0){
                System.out.print(arr[a]+qian+arr[b]+bai);
            }else if (c == 0 && d != 0){
                System.out.print(arr[a]+qian+arr[b]+bai+arr[0]+arr[d]);
            }else if(d == 0){
                System.out.print(arr[a]+qian+arr[b]+bai+arr[c]+shi);
            }else {
                System.out.print(arr[a]+qian+arr[b]+bai+arr[c]+shi+arr[d]);
            }
        }
    }
    }
    
    
  • 相关阅读:
    Windows消息机制
    inherited 为什么可以调用父类的private函数? [问题点数:100分,结帖人:h2plus0]
    C++Buidler6中需要注意的几个问题
    BGA封装芯片拆装全程纪实
    Delphi组件开发教程指南(四)组件生成过程(TWinControl)
    Delphi技巧集六 (等待执行完一个外部程序再执行另一个程序)
    C++ Builder高级应用开发指南
    干掉“Spirale”病毒
    完全看懂新世代x86指令集結構
    Delphi 组件撰写常问问题delphi 在整合环境中如何找出组件所产生的问题
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078758.html
Copyright © 2011-2022 走看看