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]);
            }
        }
    }
    }
    
    
  • 相关阅读:
    Python 语言规范(Google)
    Python 代码风格规范(Google)
    GBM,XGBoost,LightGBM
    面试编程总结
    MagicNotes:如何迈向工作的坦途
    番茄工作法:让时间变成你最好的朋友
    时间管理:如何高效地利用时间
    读点大脑科学,学会变得更聪明
    为什么我那么努力,吃了那么多苦,也没见那么优秀?(转自知乎)
    不要被懒惰夺走你的思考能力
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078758.html
Copyright © 2011-2022 走看看