题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {//写了4个小时的屎山代码,请慎重查看
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            String[] strings = spThree(s);
//            System.out.println(Arrays.toString(strings));
            ArrayList<String> sgood = new ArrayList<>();
            String zs[]=new String[]{"","thousand","million","billion"};
            for (int i = 0; i <strings.length; i++) {
                String s1 = nTl(strings[i]);
//                System.out.println(s1);
                if (s1.equals(" ")||s1.equals("")){
                    continue;
                }
                sgood.add(s1+" "+zs[i]);
            }
//            System.out.println(sgood);
            String se="";
            for (int i = sgood.size()-1; i >=0; i--) {
                se=se+sgood.get(i)+" ";
            }
            System.out.println(se);
        }
    }

    public static String[] spThree(String s) {//拆分数字方法
        char[] cy = s.toCharArray();
        String ss = "";
        for (int i = cy.length - 1; i >= 0; i--) {
            ss = ss + cy[i];
        }
        ArrayList<String> st = new ArrayList<>();
        for (int i = 0; i < ss.length(); i++) {
            String[] split = ss.split("");
            if ((i + 1) % 3 == 0) {
                st.add(split[i]);
                st.add(" ");
            } else {
                st.add(split[i]);
            }
        }
        String s1 = "";
        for (int i = 0; i < st.size(); i++) {
            s1 = s1 + st.get(i);
        }
        String[] split = s1.split(" ");
        for (int i = 0; i < split.length; i++) {
            String s2 = split[i];
            String s3 = "";
            String[] split1 = s2.split("");
            for (int j = split1.length - 1; j >= 0; j--) {
                s3 = s3 + split1[j];
            }
            split[i] = s3;
        }
        return split;
    }

    public static String nTl(String s) {//分类
        String s1 = "";
        String s2 = "";
        String s3 = "";
        if (s.length() == 1) {
            switch (s) {
                case "0":
                    s1 = "zero";
                    break;
                case "1":
                    s1 = "one";
                    break;
                case "2":
                    s1 = "two";
                    break;
                case "3":
                    s1 = "three";
                    break;
                case "4":
                    s1 = "four";
                    break;
                case "5":
                    s1 = "five";
                    break;
                case "6":
                    s1 = "six";
                    break;
                case "7":
                    s1 = "seven";
                    break;
                case "8":
                    s1 = "eight";
                    break;
                case "9":
                    s1 = "nine";
                    break;
            }
            return s1;
        } else if (s.length() == 2||(s.length()==3 && String.valueOf(s.charAt(0)).equals("0"))) {
            if (s.length()==3){
                s=s.substring(1);
            }
            if (String.valueOf(s.charAt(0)).equals("1")) {
                switch (String.valueOf(s.charAt(1))) {
                    case "0":
                        s2 = "ten";
                        break;
                    case "1":
                        s2 = "eleven";
                        break;
                    case "2":
                        s2 = "twelve";
                        break;
                    case "3":
                        s2 = "thirteen";
                        break;
                    case "4":
                        s2 = "fourteen";
                        break;
                    case "5":
                        s2 = "fifteen";
                        break;
                    case "6":
                        s2 = "sixteen";
                        break;
                    case "7":
                        s2 = "seventeen";
                        break;
                    case "8":
                        s2 = "eighteen";
                        break;
                    case "9":
                        s2 = "nineteen";
                        break;
                }
                return s2;
            } else {
                switch (String.valueOf(s.charAt(0))) {
                    case "2":
                        s2 = "twenty";
                        break;
                    case "3":
                        s2 = "thirty";
                        break;
                    case "4":
                        s2 = "forty";
                        break;
                    case "5":
                        s2 = "fifty";
                        break;
                    case "6":
                        s2 = "sixty";
                        break;
                    case "7":
                        s2 = "seventy";
                        break;
                    case "8":
                        s2 = "eighty";
                        break;
                    case "9":
                        s2 = "ninety";
                        break;
                }
                switch (String.valueOf(s.charAt(1))) {
                    case "1":
                        s1 = "one";
                        break;
                    case "2":
                        s1 = "two";
                        break;
                    case "3":
                        s1 = "three";
                        break;
                    case "4":
                        s1 = "four";
                        break;
                    case "5":
                        s1 = "five";
                        break;
                    case "6":
                        s1 = "six";
                        break;
                    case "7":
                        s1 = "seven";
                        break;
                    case "8":
                        s1 = "eight";
                        break;
                    case "9":
                        s1 = "nine";
                        break;
                }
                if (s1.equals("")){
                    return s2;
                }
                if (s2.equals("")){
                    return s1;
                }
                return s2+" "+s1;
            }
        } else if (s.length() == 3) {
            if (!String.valueOf(s.charAt(0)).equals("0")) {
                switch (String.valueOf(s.charAt(0))) {
                    case "1":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "one hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "one hundred";
                        }
                        break;
                    case "2":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "two hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "two hundred";
                        }
                        break;
                    case "3":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "three hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "three hundred";
                        }
                        break;
                    case "4":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "four hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "four hundred";
                        }
                        break;
                    case "5":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "five hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "five hundred";
                        }
                        break;
                    case "6":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "six hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "six hundred";
                        }
                        break;
                    case "7":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "seven hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "seven hundred";
                        }
                        break;
                    case "8":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "eight hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "eight hundred";
                        }
                        break;
                    case "9":
                        if (!String.valueOf(s.charAt(2)).equals("0")||(Integer.valueOf(s.substring(1))%10==0)) {
                            s3 = "nine hundred and";
                        }
                        if (Integer.valueOf(s)%100==0){
                            s3 = "nine hundred";
                        }
                }
                if (String.valueOf(s.charAt(1)).equals("1")) {
                    switch (String.valueOf(s.charAt(2))) {
                        case "0":
                            s2 = "ten";
                            break;
                        case "1":
                            s2 = "eleven";
                            break;
                        case "2":
                            s2 = "twelve";
                            break;
                        case "3":
                            s2 = "thirteen";
                            break;
                        case "4":
                            s2 = "fourteen";
                            break;
                        case "5":
                            s2 = "fifteen";
                            break;
                        case "6":
                            s2 = "sixteen";
                            break;
                        case "7":
                            s2 = "seventeen";
                            break;
                        case "8":
                            s2 = "eighteen";
                            break;
                        case "9":
                            s2 = "nineteen";
                            break;
                    }
                    return s3+" "+s2;
                } else {
                    switch (String.valueOf(s.charAt(1))) {
                        case "2":
                            s2 = "twenty";
                            break;
                        case "3":
                            s2 = "thirty";
                            break;
                        case "4":
                            s2 = "forty";
                            break;
                        case "5":
                            s2 = "fifty";
                            break;
                        case "6":
                            s2 = "sixty";
                            break;
                        case "7":
                            s2 = "seventy";
                            break;
                        case "8":
                            s2 = "eighty";
                            break;
                        case "9":
                            s2 = "ninety";
                            break;
                    }
                    switch (String.valueOf(s.charAt(2))) {
                        case "1":
                            s1 = "one";
                            break;
                        case "2":
                            s1 = "two";
                            break;
                        case "3":
                            s1 = "three";
                            break;
                        case "4":
                            s1 = "four";
                            break;
                        case "5":
                            s1 = "five";
                            break;
                        case "6":
                            s1 = "six";
                            break;
                        case "7":
                            s1 = "seven";
                            break;
                        case "8":
                            s1 = "eight";
                            break;
                        case "9":
                            s1 = "nine";
                            break;
                    }
                    if (!s2.equals("0")&&!s2.equals("")) {
                        return s3 + " " + s2 + " " + s1;
                    } else if (s1.equals("")) {
                        return s3;
                    } else {
                        return s3 + " " + s1;
                    }
                }
            }
        }
        return null;
    }
}

全部评论

相关推荐

09-29 17:44
已编辑
蔚来_测(准入职员工)
//鲨鱼辣椒:见不了了我实习了四个月上周再投筛选了一天就给我挂了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务