华为2017年8月30日校招编程真题01-数字的中文拼音和英文单词之间互相转换

题目

将数字的中文拼音和英文单词相互转换,
当有两个连续的数字时,比如“00”,可以输入”DoubleLing”或者”DoubleZero”,但是输出时不可含有”Double”。

思路

AC

package test;

import java.util.Scanner;

/**
 * description:
 *
 * @author liyazhou
 * @since 2017-08-30 18:03
 */

/**
 * 题目:
 *     将数字的中文拼音和英文单词相互转换,
 *     当有两个连续的数字时,比如“00”,可以输入"DoubleLing"或者"DoubleZero",但是输出时不可含有"Double"。
 */
public class Main {

    public static void main(String[] args){
        String[] pin = {"Ling", "Yi", "Er", "San", "Si", "Wu", "Liu", "Qi", "Ba", "Jiu"};
        String[] digit = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};

        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length(); i ++){
            if (input.charAt(i) <= 'Z' && input.charAt(i) >= 'A'){
                if (i != 0){
                    sb.append("_");
                }
                // sb.append("_");
            }
            sb.append(input.charAt(i));
        }
        String[] strs = sb.toString().split("_");
//        System.out.println(Arrays.toString(strs));
        String lastEle = strs[strs.length-1];
        boolean isDigit = false;
        for (int i = 0; i < digit.length; i ++){
            if (digit[i].equals(lastEle)){
                isDigit = true;
                break;
            }
        }

//        System.out.println(isDigit);

//        System.out.println(strs.length);
        for (int i = 0; i < strs.length-1; i ++){
            if (strs[i].equals("Double")){
                strs[i] = strs[i+1];
            }
        }

//        int[] indexs = new int[strs.length];
//        for (int i = 0; i < indexs.length; i ++)
//            indexs[i] = -1;

        if (isDigit){
            for (int i = 0; i < strs.length; i ++){
                boolean pass = false;
                for (int j = 0; j < digit.length; j ++){
                    if (strs[i].equals(digit[j])){
                        strs[i] = pin[j];
                        pass = true;
                        break;
                    }
                }
                if (!pass) {
                    System.out.println("ERROR");
                    return;
                }
            }

        }else{
            for (int i = 0; i < strs.length; i ++){
                boolean pass = false;
                for (int j = 0; j < pin.length; j ++){
                    if (strs[i].equals(pin[j])){
                        strs[i] = digit[j];
                        pass = true;
                        break;
                    }
                }
                if (!pass) {
                    System.out.println("ERROR");
                    return;
                }
            }
        }

        StringBuilder result = new StringBuilder();
        for (String ele : strs){
            result.append(ele);
        }
        System.out.println(result);
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值