首页 > 试题广场 >

学英语

[编程题]学英语
  • 热度指数:125797 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}你需要编写一个程序,使得对于输入的整数,输出其对应的英文单词。

\hspace{15pt}具体地,规则如下:
{\hspace{20pt}}_\texttt{1.}\,三位数字看成一整体,每三位数后添加一个计数单位,从小到大依次为 thousand(千)、million(百万);
{\hspace{20pt}}_\texttt{2.}\,对于百万以下、千以上的数字,通用公式为 x \textrm{ thousand } y ,其中 xy 代表数字;
{\hspace{20pt}}_\texttt{3.}\,对于百万以上的数字,通用公式为 x \textrm{ million } y \textrm{ thousand } z ,其中 xyz 代表数字;
{\hspace{20pt}}_\texttt{4.}\,每三位数内部,十位和个位之间,需要添加 and ;特别地,若百位为 0 ,则不添加。例如,1\,234 的英文为 one thousand two hundred and thirty four ,1\,034 的英文为 one thousand thirty four 。

\hspace{15pt}让我们再来看几个例子:
\hspace{23pt}\bullet\,22:twenty two ;
\hspace{23pt}\bullet\,100:one hundred
\hspace{23pt}\bullet\,145:one hundred and forty five ;
\hspace{23pt}\bullet\,1\,234:one thousand two hundred and thirty four ;
\hspace{23pt}\bullet\,8\,088:eight thousand eighty eight ;
\hspace{23pt}\bullet\,486\,669:four hundred and eighty six thousand six hundred and sixty nine ;
\hspace{23pt}\bullet\,1\,652\,510:one million six hundred and fifty two thousand five hundred and ten 。

输入描述:
\hspace{15pt}在一行上输入一个整数 n \left(1 \leqq n \leqq 2 \times 10^6\right) 代表待转换的整数。


输出描述:
\hspace{15pt}输出若干个小写的英文单词,代表转换后的结果。
示例1

输入

22

输出

twenty two
示例2

输入

2000000

输出

two million

从低到高每三位都存在列表里

import java.util.Scanner;
import java.util.ArrayList;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        long num = in.nextLong();
        ArrayList<Long> sections = new ArrayList<>();
        String result = "";
        while (num > 0) {
            sections.add(num % 1000);
            num /= 1000;
        }
        String[] tri = {"thousand", "million", "billion"};
        for (int i = 0; i < sections.size(); i++) {
            String r = readTriNum(sections.get(i).intValue());
            if (i == 0) {
                result += r;
            } else {
                result = r + " " + tri[i-1] + " " + result;
            }
        }
        System.out.print(result);
    }

    public static String readTriNum(int num) {
        String[] read = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        String[] tens = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        if (num < 20) {
            return read[num];
        } else if (num >= 20 && num < 100) {
            if (num % 10 == 0) {
                return tens[num/10-2];
            } else {
                return tens[num/10-2]+" "+read[num%10];
            }
        } else if (num % 100 == 0) {
            return read[num/100] + " hundred";
        } else {
            return read[num/100]+" hundred and "+readTriNum(num%100);
        }
    }
}
发表于 2024-09-27 11:48:21 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringJoiner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static final String[] units = {"thousand", "million", "billion"};
    private static final String[] units2 = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
    private static final String[] units4 = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        // 拼接逗号
        int[] s = new int[(line.length()+2)/3];
        // 012,345,674,346
        int j =  s.length*3 -line.length();
        for (int i = 0; i < s.length ; i++) {
            s[i] = Integer.parseInt(line.substring ((i*3-j)<0?0:i*3-j , (i+1)*3-j));
        }
        for (int i = 0; i < s.length; i++) {
            // 获取逗号隔开的数字
            int number = s[i];
            // 计算百位上的数字
            int a = number / 100;
            // 计算十位和个位的数字
            int b = number % 100;
            if (a > 0) { 
                System.out.print(units2[a]+" hundred ");                
                // 拼接百位上的数字
                if (b > 0) {
                    System.out.print("and ");
                }
            }
            if (b >= 20) { 
                System.out.print(units4[b/10]);
                if(b%10 >0){
                    System.out.print(" "+units2[b%10]);
                }
            }else{
                System.out.print(units2[b]);
            }
            if(s.length -1>i ){
                System.out.print(" "+units[s.length - 2 - i]+" ");    
            }
             
        }

    }
 

}

发表于 2024-09-08 15:14:00 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long num = in.nextLong();
        String str = parse3(num);
        System.out.println(str);
    }

    public static String parse3(long num) {
        String str = String.valueOf(num);
        if (str.length() < 4) {
            return parse2((int)(num));
        } else if (str.length()<7 & str.length()>3) {
            int NNN = (int) (num / 1000);
            int NN = (int) (num - NNN * 1000);
            String strnew = parse2(NNN) + " " + "thousand" + " " + parse2(NN);
            return strnew;
        } else if (str.length() < 10 && str.length() > 6) {
            int MMM = (int) (num / 1000000);
            int MM = (int) ((num - MMM * 1000000) / 1000);
            int M = (int) (num - MMM * 1000000 - MM * 1000);
            String strnew = parse2(MMM) + " " + "million" + " " + parse2(MM) + " " + "thousand" + " " + parse2(M);
            return strnew;
        } else {
            return "";
        }
    }

    public static String parse2(int num) {
        int NNN = num / 100;
        if (NNN == 0) {
            return parse1(num);
        } else {
            String[] str = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
            int Num = num - NNN * 100;
            String strnew;
            if(Num == 0){
                strnew = str[NNN - 1] + " " + "hundred" + " " + parse1(Num);;
            }else{
                strnew = str[NNN - 1] + " " + "hundred" + " " + "and" + " " + parse1(Num);
            }
            return strnew;
        }
    }

    public static String parse1(int num) {
        String[] str1 = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        String[] str2 = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
        if (num > 0 & num <= 19) {
            return str1[num - 1];
        } else if (num > 19 & num <= 99) {
            int N = num % 10;// 个位数
            int M = num / 10;// 十位数
            String strnew = "";
            if (N != 0) {
                strnew = str2[M - 2] + " " + str1[N - 1];
            } else {
                strnew = str2[M - 2];
            }
            return strnew;
        } else {
            return "";
        }
    }
}
发表于 2024-08-21 10:03:27 回复(0)

思路

三位三位处理,按 billion, million, thousand, hundred 的情况拆分:billion 左半部分按 million 处理, 右半部分按 million 处理;million 左半部分按 hundred 处理,右半部分按 thousand 处理;thousand 左右不分都按 hundred 处理;hundred 按单词形式处理 1 - 19 和 19 - 99 的情况,只有三位,and的情况具体判断下就行

代码

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long x = in.nextLong();
        solveBillion(x);
    }
    //1,001,001,000
    static void solveBillion(long x){
        int flag = 0;
        if (x >= 1000000000){
            flag = 1;
        }
        long t = x / 1000000000;
        solveMillion(t);
        if (flag == 1){
            pt("billion ");
        }
        solveMillion(x - t * 1000000000);
    }

    static void solveMillion(long x){
        int flag = 0;
        if (x >= 1000000){
            flag = 1;
        }
        long t = x / 1000000;
        solveHundred(t);
        if (flag == 1){
            pt("million ");
        }
        sovleThousand(x - t * 1000000);

    }

    static void sovleThousand(long x){
        int flag = 0;
        if (x >= 1000){
            flag = 1;
        }
        long t = x / 1000;
        solveHundred(t);
        if (flag == 1){
            pt("thousand ");
        }
        solveHundred(x - t * 1000);

    }

    static void solveHundred(long x){
        int x1 = (int)x / 100;
        int x2 = ((int)x - x1 * 100) / 10;
        int x3 = (int)x % 10;
        if (x2 == 1){
            x2 = x2 * 10 + x3;
            x3 = 0;
        }else if (x2 > 1){
            x2 = x2 + 18;
        } 
        if (x1 != 0 && (x2 != 0 || x3 != 0)){
            pt(mp[x1] + " hundred and ");
            if (x2 != 0){
                pt(mp[x2] + " ");
            }
            if (x3 != 0){
                pt(mp[x3] + " ");
            }
        }else if (x1 != 0 && x2 == 0 && x3 == 0){
            pt(mp[x1] + " hundred ");
        }else {
            if (x2 != 0){
                pt(mp[x2] + " ");
            }
            if (x3 != 0){
                pt(mp[x3] + " ");
            }

        }
    }

    static String[] mp = new String[]{"", "one", "two","three","four","five","six","seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen","eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy", "eighty", "ninety"};


    static void pt(Object o){
        System.out.print(o);
    }
}
发表于 2024-03-16 19:43:30 回复(0)
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String math = reader.readLine();
        //
        String[] low = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"
                        , "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen","seventeen", "eighteen", "nineteen", "twenty"
                       };
        String[] TenBits = {"", "ten", "twenty", "thirty", "forty", "fifty",
                            "sixty", "seventy", "eighty", "ninety"
                           };
        String[] unit = {"", "hundred", "thousand", "million", "billion"};
        List<String> list = new ArrayList<>();
        long shuZi = Long.parseLong(math);
        int index = 1; //单位下标
        while (shuZi != 0) {
            if (index != 1) {
                list.add(unit[index]);
            }
            long SanWei = shuZi % 1000; //先取百位数

            if (SanWei % 100 <= 20) {  //小于等于二十,直接读;
                int i = (int) SanWei % 100;
                list.add(low[i]);
                if (SanWei % 100 != 0 && SanWei > 100) {
                    list.add("and");
                }
                if (SanWei / 100 != 0) { //确认百位不为零
                    list.add("hundred");
                    list.add(low[(int) SanWei / 100]);
                }
                index++;
                shuZi = shuZi / 1000;
            } else if (SanWei % 100 >20) { //大于二十,如果各位不为零,则需在个位与十位之间添加and
                if (SanWei / 10 != 0) {
                    if ((SanWei % 100) % 10 != 0){
                        list.add(low[(int) (SanWei % 100) % 10]);
                    }
                    list.add(TenBits[(int) (SanWei % 100) / 10]);
                    if (SanWei / 100 != 0) {
                        list.add("and");
                        list.add("hundred");
                        list.add(low[(int) SanWei / 100]);
                    }
                }
                index++;
                shuZi = shuZi / 1000;
            }

        }
        Collections.reverse(list);
        System.out.println(String.join(" ", list));
    }
}
发表于 2024-03-07 22:57:59 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static HashMap<Integer, String> unitMap;
    static HashMap<Character, String> tenMap;
    static HashMap<Integer, String> level;

    static {
        unitMap = new HashMap<>();
        tenMap = new HashMap<>();
        level = new HashMap<>();
        unitMap.put(0, "");
        unitMap.put(00, "");
        unitMap.put(1, "one");
        unitMap.put(2, "two");
        unitMap.put(3, "three");
        unitMap.put(4, "four");
        unitMap.put(5, "five");
        unitMap.put(6, "six");
        unitMap.put(7, "seven");
        unitMap.put(8, "eight");
        unitMap.put(9, "nine");
        unitMap.put(10, "ten");
        unitMap.put(11, "eleven");
        unitMap.put(12, "twelve");
        unitMap.put(13, "thirteen");
        unitMap.put(14, "fourteen");
        unitMap.put(15, "fifteen");
        unitMap.put(16, "sixteen");
        unitMap.put(17, "seventeen");
        unitMap.put(18, "eighteen");
        unitMap.put(19, "nineteen");

        tenMap.put('0', "");
        tenMap.put('2', "twenty");
        tenMap.put('3', "thirty");
        tenMap.put('4', "forty");
        tenMap.put('5', "fifty");
        tenMap.put('6', "sixty");
        tenMap.put('7', "seventy");
        tenMap.put('8', "eighty");
        tenMap.put('9', "ninety");


        level.put(0, "hundred");
        level.put(1, "");
        level.put(2, "thousand");
        level.put(3, "million");
        level.put(4, "billion");

    }


    public static void main(String[] args) {
        //1652510  1000000000           11,000,000,000
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            int length = s.length();
            int count = length / 3;//3
            int remainder = length % 3;//0 1 2
            StringBuilder stringBuilder = new StringBuilder();
            //最高位处理 1,652,510中的1;11,000,000,000中的11
            if (remainder > 0) {
                String substring = s.substring(0, remainder);
                //大于20
                if (!unitMap.containsKey(Integer.valueOf(substring))) {
                    stringBuilder.append(tenMap.get(substring.charAt(0)));//十位
                    if (!"".equals(unitMap.get(Integer.valueOf(substring.charAt(1) + "")))) {
                        stringBuilder.append(" ");
                        stringBuilder.append(unitMap.get(Integer.valueOf(substring.charAt(1) + "")));//个位
                    }
                } else {
                    //小于20
                    stringBuilder.append(unitMap.get(Integer.valueOf(substring)));
                }
                stringBuilder.append(" ");
                stringBuilder.append(level.get(count + 1));
                stringBuilder.append(" ");
            }

            s = s.substring(remainder);//这里切分一下方便遍历切割
            int  initial = 0;
            //三位三位处理 billion/million/thousand
            for (int i = count; i > 0; i--) {//0 3; 3 6   2 1
                String substring = s.substring(initial, 3 * (count - i + 1));
                initial = 3 * (count - i + 1);
                //百位
                if (substring.charAt(0) > '0') {
                    stringBuilder.append(unitMap.get(Integer.valueOf(substring.charAt(0) + "")));
                    stringBuilder.append(" ");
                    stringBuilder.append(level.get(0));
                    //十位个位都为0跳过
                    if ("".equals(unitMap.get(Integer.valueOf(substring.substring(1, 3))))) {
                        continue;
                    }
                    stringBuilder.append(" ");
                    stringBuilder.append("and");
                    stringBuilder.append(" ");
                }

                //十位以及个位
                if (substring.charAt(1) >= '2') {
                    stringBuilder.append(tenMap.get(substring.charAt(1)));//十位
                    if (!"".equals(unitMap.get(Integer.valueOf(substring.charAt(2) + "")))) {
                        stringBuilder.append(" ");
                        stringBuilder.append(unitMap.get(Integer.valueOf(substring.charAt(2) + "")));//个位
                    }
                } else {
                    stringBuilder.append(unitMap.get(Integer.valueOf(substring.substring(1, 3))));
                }

                if (i >= 2) {
                    stringBuilder.append(" ");
                    stringBuilder.append(level.get(i));
                    stringBuilder.append(" ");
                }
            }

            System.out.println(stringBuilder);
        }
    }
}

发表于 2024-01-11 16:23:48 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringJoiner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static final String[] units = {"thousand", "million", "billion"};
    private static final String[] units2 = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    private static final String[] units3 = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
    private static final String[] units4 = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();

        // 拼接逗号
        StringBuilder sb = new StringBuilder();
        int cnt = 1;
        for (int i = line.length() - 1; i >= 0; i--) {
            char c = line.charAt(i);
            if (cnt % 3 == 0 && i != 0) {
                sb.append(c).append(",");
            } else {
                sb.append(c);
            }
            cnt++;
        }

        // 拼接逗号后的金额
        String money = sb.reverse().toString();

        String[] split = money.split(",");

        StringJoiner result = new StringJoiner(" ", "", "");
        for (int i = 0; i < split.length; i++) {
            // 获取逗号隔开的数字
            int number = Integer.parseInt(split[i]);
            if (number >= 100) {
                // 计算百位上的数字
                int a = number / 100;
                // 计算十位和个位的数字
                int b = number % 100;
                // 拼接百位上的数字
                result.add(getEnglishMoney(a));
                result.add("hundred");
                if (b > 0) {
                    result.add("and");
                }
                // 拼接十位和个位上的数字
                result.add(getEnglishMoney(b));
            } else {
                result.add(getEnglishMoney(number));
            }

            if ((split.length - 1 - i - 1) >= 0) {
                // 计算逗号对应的单位
                String unit = units[split.length - 1 - i - 1];
                result.add(unit);
            }

        }

        System.out.println(result);

    }

    /**
     * 拼接十位和各位上的数字为英文字符串
     *
     * @param b
     * @return
     */
    private static String getEnglishMoney(int b) {
        StringJoiner result = new StringJoiner(" ", "", "");
        if (b >= 20) {
            int c = b / 10;
            int d = b % 10;
            result.add(units4[c]);
            if (d > 0) {
                result.add(units2[d]);
            }
        } else if (b > 10) {
            result.add(units3[b - 10]);
        } else {
            result.add(units2[b]);
        }

        return result.toString();
    }
}

发表于 2023-08-09 18:32:34 回复(0)
1、首先将录入的数值的长度取余3,得到的余数为,最高数量级的长度,将每个数量级前的数字存入数组array中
2、将每个数量级前的数字转成英文,规则:第一位和第三位数字,除0外直接转成英文;第二位分0,1,其他,0不翻译,1特殊情况,为10~19,其他情况为几十+个位数
3、将英文加上数量级,题设最高数量级为十亿,对应的是第三个小数点,即数组有4个成员变量,数量级数组设置成{"billion","million","thousand"},拼接数量级,起始下标为4-array长度
注意点:
1、最高数量级为1位,且为0,只有数字0这一种情况,其他情况下,0一律不翻译
2、百位是否为0,与and连接词出现的情况
3、空格拼接的情况
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    int len = str.length();
    //        判断输入值的位数是否是3的倍数
    int index = len %3;
    ArrayList<String> array = new ArrayList<>();
    //        example: 11,111,111
    if(index!=0){
        String s = str.substring(0,index);
        array.add(s);
    }
    //        剩下部分添加到集合中
    while(index<len){
        String s1 = str.substring(index,index+3);
        array.add(s1);
        index+=3;
    }
    int length = array.size();
    //        千、百万、十亿对应三个逗号,更具集合的长度,可以知道输入值的最大量级
    int startIndex = 4-length;
    String[] level = new String[]{"billion","million","thousand"};
    StringBuilder ans =new StringBuilder();
    //        每个逗号之间数字转成英文,后面加上对应的数量级
    for(int i=0;i<length;i++){
        String p = part(array.get(i));
        ans.append(p);
        if(startIndex<3){
            ans.append(" "+ level[startIndex++]+ " ");
        }
    }
    System.out.println(ans.toString());
    //        while(true){
    //        }
}

//    逗号之间的三位数字对应的英文
//    长度为1或者2的时候,对应的时最大数量级的数字
public static String part(String str){
    int len = str.length();
    StringBuilder sb = new StringBuilder();
    //        只有一位:为最高位时,不可能为0;数字0,才为0
    if(len == 1){
        char ch = str.charAt(0);
        if(ch == '0'){
            return "zero";
        }
        String s = numberToEnglish(ch);
        sb.append(s);
    }
    //        最高位数字,第一位不可能为0,只有1位特殊情况
    if(len ==2){
        char ch1 =str.charAt(0);
        if(ch1 != '1'){
            String s1= numberToEnglish1(ch1);
            sb.append(s1);
            char ch2 = str.charAt(1);
            if(ch2 != '0'){
                String s2 = numberToEnglish(ch2);
                sb.append(" ");
                sb.append(s2);
            }
        }else{
            String s3 = numberToEnglish2(str);
            sb.append(s3);
        }
    }
    //        一般情况
    if(len == 3){
        char ch1 = str.charAt(0);
        //            1、百位为0
        if(ch1 != '0'){
            String s1 = numberToEnglish(ch1);
            sb.append(s1);
            sb.append(" ");
            sb.append("hundred");
        }
        char ch2 = str.charAt(1);
        //            十位非0、1
        if(ch2 != '0' && ch2!='1'){
            if(ch1 != '0'){
                sb.append(" and ");
            }
            String s2 = numberToEnglish1(ch2);
            sb.append(s2);
        }else if(ch2 == '1'){ //特殊情况:十位为1,10~19
            String ss = str.substring(1,3);
            String s3 = numberToEnglish2(ss);
            sb.append(" and ");
            sb.append(s3);
            return sb.toString();
        }
        char ch3 = str.charAt(2);
        if(ch3 != '0'){
            String s4 = numberToEnglish(ch3);
            //                百位不为0,十位为0,加and链接百位和个位
            if(ch1 != '0' && ch2 == '0'){
                sb.append(" and ");
            }
            //                十位不为0,and连接词在百位和十位之间
            if(ch2 != '0'){
                sb.append(" ");
            }
            sb.append(s4);
        }
    }
    return sb.toString();
}

//    针对三位数字的第一位和第三位
//    注意:数字0不转义,只有数字0的时候,返回zero
public static String numberToEnglish(char ch){
    String str ="";
    switch(ch){
        case '0':
            break;
        case '1':
            str = "one";
            break;
        case '2':
            str = "two";
            break;
        case '3':
            str = "three";
            break;
        case '4':
            str = "four";
            break;
        case '5':
            str = "five";
            break;
        case '6':
            str = "six";
            break;
        case '7':
            str = "seven";
            break;
        case '8':
            str = "eight";
            break;
        case '9':
            str = "nine";
            break;
    }
    return str;
}

//    针对第二位数字是非0、1情况的转义
public static String numberToEnglish1(char ch){
    String str ="";
    switch(ch){
        case '0':
            break;
        case '1':
            break;
        case '2':
            str = "twenty";
            break;
        case '3':
            str = "thirty";
            break;
        case '4':
            str = "forty";
            break;
        case '5':
            str = "fifty";
            break;
        case '6':
            str = "sixty";
            break;
        case '7':
            str = "seventy";
            break;
        case '8':
            str = "eighty";
            break;
        case '9':
            str = "ninety";
            break;
    }
    return str;
}

//    针对第二位数字的特殊情况:十位为1,范围是10~19
public static String numberToEnglish2(String s){
    String str ="";
    switch(s){
        case "10":
            str ="ten";
            break;
        case "11":
            str = "eleven";
            break;
        case "12":
            str = "twelve";
            break;
        case "13":
            str = "thirteen";
            break;
        case "14":
            str = "fourteen";
            break;
        case "15":
            str = "fifteen";
            break;
        case "16":
            str = "sixteen";
            break;
        case "17":
            str = "seventeen";
            break;
        case "18":
            str = "eighteen";
            break;
        case "19":
            str = "nineteen";
            break;
    }
    return str;
}

发表于 2023-05-31 22:38:14 回复(0)
//获取一位数或两位数的英文值
    //注意几个问题:1.英文eighty eighteen fifty fifteen forty forteen thirty thirteen twenty twelve eleven ten 的写法;2.注意大于20后如果个位有数据,则要在中间加空格;
    //3.hundred 后面的几十的数如果大于0,才加and,同理,hundred和几十加起来大于0,才显示thousand
    public static String getNumStr(long num) {
        StringBuffer str = new StringBuffer();
        if (num / 10 == 9) {
            str.append("ninety");
        } else if (num / 10 == 8) {
            str.append("eighty");
        } else if (num / 10 == 7) {
            str.append("seventy");
        } else if (num / 10 == 6) {
            str.append("sixty");
        } else if (num / 10 == 5) {
            str.append("fifty");
        } else if (num / 10 == 4) {
            str.append("forty");
        } else if (num / 10 == 3) {
            str.append("thirty");
        } else if (num / 10 == 2) {
            str.append("twenty");
        } else if (num / 10 == 1) {
            if (num % 10 == 9) {
                str.append("nineteen");
            } else if (num % 10 == 8) {
                str.append("eighteen");
            } else if (num % 10 == 7) {
                str.append("seventeen");
            } else if (num % 10 == 6) {
                str.append("sixteen");
            } else if (num % 10 == 5) {
                str.append("fifteen");
            } else if (num % 10 == 4) {
                str.append("forteen");
            } else if (num % 10 == 3) {
                str.append("thirteen");
            } else if (num % 10 == 2) {
                str.append("twelve");
            } else if (num % 10 == 1) {
                str.append("eleven");
            } else if (num % 10 == 0) {
                str.append("ten");
            }
        }
        if(num>20 && num%10 !=0){ //大于20,并且不是整除,加个空格
            str.append(" ");
        }
        if (num / 10 != 1) {
            if (num % 10 == 9) {
                str.append("nine");
            } else if (num % 10 == 8) {
                str.append("eight");
            } else if (num % 10 == 7) {
                str.append("seven");
            } else if (num % 10 == 6) {
                str.append("six");
            } else if (num % 10 == 5) {
                str.append("five");
            } else if (num % 10 == 4) {
                str.append("four");
            } else if (num % 10 == 3) {
                str.append("three");
            } else if (num % 10 == 2) {
                str.append("two");
            } else if (num % 10 == 1) {
                str.append("one");
            }
        }
        return str.toString();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLong()) { // 注意 while 处理多个 case
            long a = in.nextLong();
            StringBuffer bf = new StringBuffer();
            if (a < 100) {
                bf.append(getNumStr(a));
            } else if (a < 1000000) {//542123
                long jbq = (long)(a / 1000 / 100); //几百千5
                long jsq = (long)(a / 1000 % 100); //几十千42
                long jb = (long)(a % 1000 / 100); //几百1
                long js = (long)(a % 1000 % 100); //几十23
 
                if (jbq > 0) {
                    bf.append(getNumStr(jbq)).append(" hundred");
                    if(jsq > 0){
                        bf.append(" and ");//当后面有数据再加and
                    }
                }
                if (jsq > 0) {
                    bf.append(getNumStr(jsq));
                }
                if ((jbq+jsq) > 0) { //前面两个数加起来大于0,则加入thousand
                    bf.append(" thousand ");
                }
                if (jb > 0) {
                    bf.append(getNumStr(jb)).append(" hundred");
                     if(js > 0){
                        bf.append(" and ");////当后面有数据再加and
                    }
                }
                if (js > 0) {
                    bf.append(getNumStr(js));
                }
            } else if (a <= 2000000) {//1123456
                long jm = (long)(a / 1000 / 1000); //几个million 1
                 a = (long)(a % 1000000);//123456
                long jbq = (long)(a / 1000 / 100); //几百千1
                long jsq = (long)(a / 1000 % 100); //几十千23
                long jb = (long)(a % 1000 / 100); //几百4
                long js = (long)(a % 1000 % 100); //几十56
               
                if(jm>0){
                     bf.append(getNumStr(jm)).append(" million").append(" ");
                }
                if (jbq > 0) {
                    bf.append(getNumStr(jbq)).append(" hundred");
                     if(jsq > 0){
                        bf.append(" and ");////当后面有数据再加and
                    }
                }
                if (jsq > 0) {
                    bf.append(getNumStr(jsq));
                }
                if ((jbq+jsq) > 0) { ////前面两个数加起来大于0,则加入thousand
                    bf.append(" thousand ");
                }

                if (jb > 0) {
                    bf.append(getNumStr(jb)).append(" hundred");
                     if(js > 0){
                        bf.append(" and ");////当后面有数据再加and
                    }
                }
                if (js > 0) {
                    bf.append(getNumStr(js));
                }
            }
            System.out.println(bf.toString());
        }
    }
发表于 2023-04-19 00:01:54 回复(0)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        String[] english20  = {"one","two","three","four","five","six","seven","eight","nine"
                             ,"ten","eleven","twelve","thirteen","forteen","fifteen","sixteen","seventeen"
                             ,"eighteen","nineteen"};
        String[] english100 = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
        String[] unit = {"billion ","million ","thousand ",""};
        int j = str.length()/3 , k = str.length()%3 , i = str.length() , m = 3;
        String result = "";
        if(i >= 3){
            while(j>0){
                result = translate(str.substring(i-3,i),english20,english100)+unit[m]+result;
                m--;
                i-=3;
                j--;
            }    
        }
        if(k!=0)
        result = translate(str.substring(0,k),english20,english100)+unit[m]+result;
        
        System.out.print(result);
    }
    
    public static String translate(String s,String[] eng20,String[] eng100){
        String res = "";
        int in = Integer.parseInt(s);
        if(in >= 100){
            res += eng20[in/100-1]+" hundred ";
            if(in%100!= 0)res += "and ";
        }
        if(in%100!= 0){
            int d = in % 100;
            if(d<20){
                res += eng20[d-1]+" ";
            }else{
                res += eng100[d/10-2]+" ";
                if(d%10!=0)res += eng20[d%10-1]+" ";
            }
        }
        
        return res;
    }
}
发表于 2022-09-16 03:00:29 回复(1)
我真是个弱智,11~19恁是写成了ten one~ten nine,哭辽
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static final String[] GW = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    public static final String[] SW = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    public static final String[] FW = {"ten", "elven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen ", "eighteen ", "nineteen"};
    public static final String[] UNIT = {" billion ", " million ", " thousand ", " hundred ", " and "};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        long num = Long.parseLong(str);
        StringBuilder sb = new StringBuilder();
        for (int i = str.length() - 1, k = 0; i >= 0; i--) {
            k++;
            sb.append(str.charAt(i));
            if (k % 3 == 0 && i != 0) {
                sb.append(",");
            }
        }
        String newStr = sb.reverse().toString();
        String[] strArr = newStr.split(",");
        String result = result(strArr);
        System.out.println(result);
    }

    public static String result(String[] arr) {
        int len = arr.length;
        StringBuilder sb = new StringBuilder();
        if (len == 4) {
            sb.append(transfer(arr[0]));
            sb.append(UNIT[0]);
            sb.append(transfer(arr[1]));
            sb.append(UNIT[1]);
            sb.append(transfer(arr[2]));
            sb.append(UNIT[2]);
            sb.append(transfer(arr[3]));
        } else if (len == 3) {
            sb.append(transfer(arr[0]));
            sb.append(UNIT[1]);
            sb.append(transfer(arr[1]));
            sb.append(UNIT[2]);
            sb.append(transfer(arr[2]));
        } else if (len == 2) {
            sb.append(transfer(arr[0]));
            sb.append(UNIT[2]);
            sb.append(transfer(arr[1]));
        } else {
            sb.append(transfer(arr[0]));
        }
        return sb.toString();
    }

    public static String transfer(String str) {
        int num = Integer.parseInt(str);
        str = String.valueOf(num);
        StringBuilder sb = new StringBuilder();
        int len = str.length();
        if (len == 3) {
            int i = Integer.parseInt(String.valueOf(str.charAt(0)));
            int j = Integer.parseInt(String.valueOf(str.charAt(1)));
            int k = Integer.parseInt(String.valueOf(str.charAt(2)));
            sb.append(GW[i]);
            if (j >= 2 && k != 0) {
                sb.append(UNIT[3]);
                sb.append("and ");
                sb.append(SW[j]);
                sb.append(" ");
                sb.append(GW[k]);
            } else if (j == 1 && k != 0) {
                sb.append(UNIT[3]);
                sb.append("and ");
                sb.append(FW[k]);
            } else if (j != 0 && k == 0) {
                sb.append(UNIT[3]);
                sb.append("and ");
                sb.append(SW[j]);
            } else if (j == 0 && k != 0) {
                sb.append(UNIT[3]);
                sb.append("and ");
                sb.append(GW[k]);
            } else {
                sb.append(" hundred");
            }
        } else if (len == 2) {
            int i = Integer.parseInt(String.valueOf(str.charAt(0)));
            int j = Integer.parseInt(String.valueOf(str.charAt(1)));

            if (i >= 2) {
                sb.append(SW[i]);
                if(j != 0){
                    sb.append(" " + GW[j]);
                }
            } else if(i == 1){
                sb.append(FW[j]);
            }
        } else {
            int i = Integer.parseInt(String.valueOf(str.charAt(0)));
            sb.append(GW[i]);
        }
        return sb.toString();
    }
}


发表于 2022-08-15 22:18:40 回复(0)
仅供参考
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {

    private static String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven",
                                    "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
                                    "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"
                                   };

    private static String[] tows = {"", "ten", "twenty", "thirty", "forty", "fifty",
                                    "sixty", "seventy", "eighty", "ninety"
                                   };

    private static String[] power = {"", "hundred", "thousand", "million", "billion"};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String nextLine = scanner.nextLine();

        List<String> list = new ArrayList<>();
        for (int i = nextLine.length() - 1; i >= 0; i--) {
            StringBuilder builder = new StringBuilder();
            for (int j = 0; j < 3; j++) {
                if (i - j >= 0) {
                    builder.append(nextLine.charAt(i - j));
                }
            }
            i = i - 2;
            list.add(builder.toString());
        }

        StringBuilder ans = new StringBuilder();

        for (int i = 0; i < list.size(); i++) {
            String numSplit = list.get(i);
            for (int j = 0; j < numSplit.length(); j++) {
                if (j == 0 ) {
                    if (i != 0 ) {
                        if (list.get( i - 1).charAt(2) != '0' || list.get( i - 1).charAt(1) != '0' ||
                                list.get( i - 1).charAt(0) != '0') {
                            ans.append(" ");
                        }
                        ans.append(power[i + 1] + " ");
                    }
                    ans.append(ones[numSplit.charAt(j) - 48]);
                }

                if (j == 1 ) {
                    if (!"".equals(tows[numSplit.charAt(j) - 48])) {
                        if (!"".equals(ones[numSplit.charAt(0) - 48])) {
                            ans.append(" ");
                        }
                        ans.append( tows[numSplit.charAt(j) - 48]);
                    }
                }

                if (j == 2) {
                    if (numSplit.charAt(j) != '0') {
                        if (!"".equals(ones[numSplit.charAt(0) - 48]) ||
                                !"".equals(ones[numSplit.charAt(1) - 48])) {
                            ans.append(" " + "and" + " ");
                        }
                        ans.append(power[1] + " " + ones[numSplit.charAt(j) - 48]);
                    }
                }
            }
        }


        String[] res = ans.toString().split(" ");

        if (list.get(0).length() >= 2) {
            String substring = list.get(0).substring(0, 2);
            StringBuilder builder = new StringBuilder(substring);
            StringBuilder reverse = builder.reverse();
            int i = Integer.parseInt(reverse.toString());
            if (i < 20 && i > 10) {
                res[0] = ones[i];
                res[1] = "";
            }
        }


        for (int i = res.length - 1 ; i >= 0; i--) {
            System.out.print(res[i]);

            if ( i != 0 && !"".equals(res[i])) {
                System.out.print( " " );
            }
        }

        if (Long.parseLong(nextLine) == 0) {
            System.out.println("zero");
        }
    }
}


发表于 2022-08-06 22:17:51 回复(0)
import java.util.*;

public class Main {
    static String[] to19 = new String[]{"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    static String[] tens = new String[]{"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety","hundred"};
    static String[] hundreds = {"", "thousand", "million", "billion"};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        Stack<String> stk = new Stack<>();
        StringBuilder stb = new StringBuilder();
        while (n > 0) {
            long m = n % 1000;
            if (m >= 100) {
                stb.append(to19[(int)m/100]).append(" ").append(tens[10]).append(" ");
            }
            if(m%100>0){
                if(stb.length()>0) stb.append("and ");
                if(m%100>=20){
                    stb.append(tens[(int) ((m%100)/10)]).append(" ");
                    if(m%10>0){
                        stb.append(to19[(int) (m%10)]).append(" ");;
                    }
                }else{
                    stb.append(to19[(int) (m%100)]).append(" ");;
                }
            }
            stb.append("");
            n/=1000;
            stk.push(stb.toString());
            stb.delete(0,stb.length());
        }
        while (stk.size()>0) {
            stb.append(stk.pop()).append(hundreds[stk.size()]).append(" ");
        }
        System.out.println(stb.substring(0,stb.length()-1));
    }


}

发表于 2022-07-06 22:53:46 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();

        int count = line.length() % 3 == 0 ? line.length()/3 : line.length()/3 + 1;
        List<String> list1 = new ArrayList<>();

        int end = line.length();
        for (int i = 1; i <= count; i++) {

            String s = line.substring(end-3 < 0 ? 0:end-3, end);
            list1.add(s);
            if (i != count){
                list1.add(",");
            }
            end = end-3;
        }
        StringBuilder sb1 = new StringBuilder();
        for (int i = list1.size()-1; i >= 0 ; i--) {
            sb1.append(list1.get(i));
        }



        String[] split = sb1.toString().split(",");
        List<String> list = Arrays.asList(split);
        HashMap<String, String> geweishumap = new HashMap<>();
        geweishumap.put("1","one");
        geweishumap.put("2","two");
        geweishumap.put("3","three");
        geweishumap.put("4","four");
        geweishumap.put("5","five");
        geweishumap.put("6","six");
        geweishumap.put("7","seven");
        geweishumap.put("8","eight");
        geweishumap.put("9","nine");
        geweishumap.put("10","ten");

        HashMap<String, String> shiweishumap = new HashMap<>();
        shiweishumap.put("10","ten");
        shiweishumap.put("11","eleven");
        shiweishumap.put("12","twelve");
        shiweishumap.put("13","thirteen");
        shiweishumap.put("14","fourteen");
        shiweishumap.put("15","fifteen");
        shiweishumap.put("16","sixteen");
        shiweishumap.put("17","seventeen ");
        shiweishumap.put("18","eighteen");
        shiweishumap.put("19","nineteen");

        HashMap<String, String> shiweishumap2 = new HashMap<>();
        shiweishumap2.put("2","twenty");
        shiweishumap2.put("3","thirty");
        shiweishumap2.put("4","forty");
        shiweishumap2.put("5","fifty");
        shiweishumap2.put("6","sixty");
        shiweishumap2.put("7","seventy");
        shiweishumap2.put("8","eighty");
        shiweishumap2.put("9","ninety");


        StringBuilder sb = new StringBuilder();
        String first = null;
        String second = null;
        String third = null;
        switch (list.size()){
            case 3:
                first = list.get(0);
                String s = geweishumap.get(first);
                sb.append(s+" "+"million ");

                second = list.get(1);
                char c = second.charAt(0);

                String s1 = geweishumap.get(String.valueOf(c));
                if (s1 != null && !s1.equals("")){
                    sb.append(s1+" "+"hundred");
                }


                char c1 = second.charAt(1);
                char c2 = second.charAt(2);

                if (c!='0' && (c1 != '0' || c2 != '0')){
                    sb.append(" and ");
                }

                if (c1 == '1'){
                    String num = second.substring(1, 3);
                    String s2 = shiweishumap.get(num);
                    sb.append(s2);
                }else {
                    String s2 = shiweishumap2.get(String.valueOf(c1));
                    if (s2 != null && !s2.equals("")){
                        sb.append(s2);
                    }
                    String s3 = geweishumap.get(String.valueOf(c2));
                    if (s3 != null && !s3.equals("")){
                        if (s2 != null){
                            sb.append(" ");
                        }
                        sb.append(s3);
                    }
                }
                sb.append(" thousand ");

                third = list.get(2);

                char baiwei = third.charAt(0);
                String baiweiStr = geweishumap.get(String.valueOf(baiwei));
                if (baiweiStr != null && !baiweiStr.equals("")){
                    sb.append(baiweiStr+" "+"hundred");
                }

                char shiwei = third.charAt(1);
                char gewei = third.charAt(2);

                if (baiwei != '0' && (shiwei != '0' || gewei != '0')){
                    sb.append(" and ");
                }

                if (shiwei == '1'){
                    String num = third.substring(1, 3);
                    String s2 = shiweishumap.get(num);
                    sb.append(s2);
                }else {
                    String s2 = shiweishumap2.get(String.valueOf(shiwei));
                    if (s2 != null && !s2.equals("")){
                        sb.append(s2);
                    }
                    String s3 = geweishumap.get(String.valueOf(gewei));
                    if (s3 != null && !s3.equals("")){
                        if (s2 != null){
                            sb.append(" ");
                        }
                        sb.append(s3);
                    }
                }
                System.out.println(sb.toString());

                break;
            case 2:
                first = list.get(0);
                switch (first.length()){
                    case 3:
                        char c2_3_0 = first.charAt(0);
                        String str2_3_0 = geweishumap.get(String.valueOf(c2_3_0));
                        if (str2_3_0 != null){
                            sb.append(str2_3_0+" "+"hundred");
                        }
                        char c2_3_1 = first.charAt(1);
                        char c2_3_3 = first.charAt(2);

                        if (c2_3_0 != '0' &&(c2_3_1!= '0' ||c2_3_3!= '0')){
                            sb.append(" and ");
                        }


                        if (c2_3_1 == '1'){
                            String num = first.substring(1, 3);
                            String s2 = shiweishumap.get(num);
                            sb.append(s2);
                        }else {
                            String s2 = shiweishumap2.get(String.valueOf(c2_3_1));
                            if (s2 != null && !s2.equals("")){
                                sb.append(s2);
                            }
                            String s3 = geweishumap.get(String.valueOf(c2_3_3));
                            if (s3 != null && !s3.equals("")){
                                if (s2 != null){
                                    sb.append(" ");
                                }
                                sb.append(s3);
                            }
                        }

                        break;
                    case 2:
                        char c2_2_0 = first.charAt(0);
                        char c2_2_1 = first.charAt(1);

                        if (c2_2_0 == '1'){

                            String s2 = shiweishumap.get(first);
                            sb.append(s2);
                        }else {
                            String s2 = shiweishumap2.get(String.valueOf(c2_2_0));
                            if (s2 != null && !s2.equals("")){
                                sb.append(s2);
                            }
                            String s3 = geweishumap.get(String.valueOf(c2_2_1));
                            if (s3 != null && !s3.equals("")){
                                if (s2 != null){
                                    sb.append(" ");
                                }
                                sb.append(s3);
                            }
                        }

                        break;
                    case 1:
                        char c2_1_0 = first.charAt(0);
                        String s3 = geweishumap.get(String.valueOf(c2_1_0));
                        if (s3 != null && !s3.equals("")){
                            sb.append(s3);
                        }
                        break;
                }
                sb.append(" thousand ");

                second = list.get(1);

                char baiwei_2 = second.charAt(0);
                String baiweiStr_2 = geweishumap.get(String.valueOf(baiwei_2));
                if (baiweiStr_2 != null && !baiweiStr_2.equals("")){
                    sb.append(baiweiStr_2+" "+"hundred");
                }

                char shiwei_2 = second.charAt(1);
                char gewei_2 = second.charAt(2);

                if (baiwei_2 != '0' && (shiwei_2 != '0' || shiwei_2 != '0')){
                    sb.append(" and ");
                }

                if (shiwei_2 == '1'){
                    String num = second.substring(1, 3);
                    String s2 = shiweishumap.get(num);
                    sb.append(s2);
                }else {
                    String s2 = shiweishumap2.get(String.valueOf(shiwei_2));
                    if (s2 != null && !s2.equals("")){
                        sb.append(s2);
                    }
                    String s3 = geweishumap.get(String.valueOf(gewei_2));
                    if (s3 != null && !s3.equals("")){
                        if (s2!=null){
                            sb.append(" ");
                        }
                        sb.append(s3);
                    }
                }
                System.out.println(sb.toString());

                break;
            case 1:
                first = list.get(0);
                switch (first.length()){
                    case 3:
                        char c1_3_0 = first.charAt(0);
                        String str1_3_0 = geweishumap.get(String.valueOf(c1_3_0));
                        if (str1_3_0 != null){
                            sb.append(str1_3_0+" "+"hundred");
                        }
                        char c1_3_1 = first.charAt(1);
                        char c1_3_2 = first.charAt(2);

                        if (c1_3_0!= '0' && (c1_3_1 != '0' || c1_3_2 != '0')){
                            sb.append(" and ");
                        }

                        if (c1_3_1 == '1'){
                            String num = first.substring(1, 3);
                            String s2 = shiweishumap.get(num);
                            sb.append(s2);
                        }else {
                            String s2 = shiweishumap2.get(String.valueOf(c1_3_1));
                            if (s2 != null && !s2.equals("")){
                                sb.append(s2);
                            }
                            String s3 = geweishumap.get(String.valueOf(c1_3_2));
                            if (s3 != null && !s3.equals("")){
                                if (s2 != null){
                                    sb.append(" ");
                                }
                                sb.append(s3);
                            }
                        }

                        break;
                    case 2:
                        char c1_2_0 = first.charAt(0);
                        char c1_2_1 = first.charAt(1);

                        if (c1_2_0 == '1'){

                            String s2 = shiweishumap.get(first);
                            sb.append(s2);
                        }else {
                            String s2 = shiweishumap2.get(String.valueOf(c1_2_0));
                            if (s2 != null && !s2.equals("")){
                                sb.append(s2);
                            }
                            String s3 = geweishumap.get(String.valueOf(c1_2_1));
                            if (s3 != null && !s3.equals("")){
                                if (s2 != null){
                                    sb.append(" ");
                                }
                                sb.append(s3);
                            }
                        }
                        break;
                    case 1:
                        char c1_1_0 = first.charAt(0);
                        String s3 = geweishumap.get(String.valueOf(c1_1_0));
                        if (s3 != null && !s3.equals("")){
                            sb.append(s3);
                        }
                        break;
                }
                System.out.println(sb.toString());
                break;
            default:
                break;
        }
    }
}


分类硬写
发表于 2022-06-16 22:30:30 回复(0)
import java.util.*;

public class Main {

    private static String[] nums1 = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                                    "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen"};

    private static String[] nums2 = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

    private static String[] nums3 = {"thousand", "million", "billion"};



    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String input = sc.next();
        StringBuilder result = new StringBuilder();

        if (input.length() > 6) {
            result.append(convert(input.substring(0, input.length() - 6), "million"));
            input = input.substring(input.length() - 6);
        }
        if (input.length() > 3) {
            result.append(convert(input.substring(0, input.length() - 3), "thousand"));
            input = input.substring(input.length() - 3);
        }
        if (input.length() > 0) {
            result.append(convert(input, ""));
        }

        System.out.println(result);

    }



    private static String convert(String valueStr, String flag) {

        int value = Integer.valueOf(valueStr);
        if (value == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        if ((value / 100) != 0) {
            sb.append(nums1[value / 100]).append(" hundred ");
            value = value % 100;
            if (value != 0) {
                sb.append("and ");
            }
        }
        boolean ignore = false;
        if ((value / 10) != 0) {
            if (value < 20){
                sb.append(nums1[value]);
                ignore = true;
            } else {
                sb.append(nums2[value / 10 - 1]);
            }
            if (value == 11 || value == 12) {
                ignore = true;
            }
            value = value % 10;
            if (value != 0 && !ignore) {
                sb.append(" ");
            }
        }
        if (value != 0) {
            if (!ignore) {
                sb.append(nums1[value]);
            }
        }

        sb.append(" " + flag + " ");

        return sb.toString();
    }

}


发表于 2022-06-04 12:28:56 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String[] underTwenty = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
                "twenty" };
        String[] nuliplesOfTen = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
                "ninety" };
        String[] unit = { "", "thousand", "million", "billion" };
        String hundred = "hundred";
        String and = "and";
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int input = Integer.parseInt(scanner.nextLine());

            List<Integer> numList = new ArrayList<>();
            while (input != 0) {
                numList.add(input % 1000);
                input /= 1000;
            }
            String result = "";
            for (int i = 0; i < numList.size(); i++) {
                String temp = "";
                int num = numList.get(i);

                if (num >= 100) {
                    temp = underTwenty[num / 100] + " " + hundred + " ";
                    num = num % 100;
                }

                if (num != 0 && !temp.isEmpty()) {
                    temp = temp + and + " ";
                }

                if (num > 20) {
                    temp = temp + nuliplesOfTen[num / 10] + " ";
                    num = num % 10;
                }

                if (num != 0) {
                    temp = temp + underTwenty[num] + " ";
                }

                if (!temp.isEmpty() && i != 0) {
                    temp = temp + unit[i];
                }
                result = temp.trim() + " " + result;
            }
            System.out.println(result);
        }
    }
}

发表于 2022-04-19 20:38:56 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        // 预设字典
        String[] dic1 = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        String[] dic2 = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        String[] dic3 = {"zero","ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        String[] dic4 = {"", "thousand", "million", "billion"};
        while(in.hasNextLine()){
            // 输入的处理
            String num = in.nextLine();
            StringBuilder res = new StringBuilder();
            String zero = "";
            if((num.length() % 3) != 0){
                for(int i = 0; i < (3 - num.length() % 3); i++){
                    zero += "0";
                }
            }
            num = zero + num;
            // 每三个一位进行处理,最多处理到billion
            int n = num.length();
            int i = 0;
            List<String> list = new ArrayList<>();
            while(n >= 3){
                StringBuilder builder = new StringBuilder();
                String str = num.substring(n - 3, n);
                int a = str.charAt(0) - '0';
                int b = str.charAt(1) - '0';
                int c = str.charAt(2) - '0';
                boolean aIsZero = false;
                boolean bIsZero = false;
                boolean cIsZero = false;
                // 百位
                if(a != 0){
                    builder.append(dic1[a] + " hundred");
                }else{
                    aIsZero = true;
                }
                if(!aIsZero && (b != 0 || c != 0)){
                    builder.append(" and");
                }
                String kongge = aIsZero ? "" : " ";
                // 十位
                if(b != 0){
                    if(b == 1){
                        int temp = b * 10 + c - 10;
                        builder.append(kongge + dic2[temp]);
                    }else{
                        builder.append(kongge + dic3[b] + (c == 0 ? "" : (" " + dic1[c])));
                    }
                }else{
                    bIsZero = true;
                }
                // 个位
                if(c != 0){
                    if(bIsZero){
                        builder.append(kongge + dic1[c]);
                    }
                }else{
                    cIsZero = true;
                }
                // 最后的单位
                if(!aIsZero || !bIsZero || !cIsZero){
                    builder.append((i == 0 ? "" : " ") + dic4[i]);
                    list.add(builder.toString());
                }
                // 更新索引
                n -= 3;
                i++;
            }
            // 输出的处理
            for(int j = list.size() - 1; j >= 0; j--){
                if(j != 0){
                    res.append(list.get(j) + " ");
                }else{
                    res.append(list.get(j));
                }
            }
            System.out.println(res);
        }
    }
}

发表于 2022-04-09 20:03:10 回复(0)
import java.util.Scanner;
public class Main {
    public static String[] num1 = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    public static String[] num2 = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen","seventeen", "eighteen", "nineteen" };
    public static String[] num3 = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty","ninety" };
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            long n = sc.nextLong();
            StringBuilder sb = new StringBuilder();
            long billion = n / 1000000000;
            if (billion > 0) {
                sb.append(trans(billion) + " billion ");
            }
            n = n % 1000000000;//10亿后面的数字
            long million = n / 1000000;
            if (million > 0) {
                sb.append(trans(million) + " million ");
            }
            n = n % 1000000;//百万后面的数字
            long thousand = n / 1000;
            if (thousand > 0) {
                sb.append(trans(thousand) + " thousand ");
            }
            n = n % 1000;//后三位数字
            sb.append(trans(n));
            System.out.println(sb.toString().trim());
        }
    }
    public static String trans(long l) {
        int n = (int) l;
        StringBuilder sb = new StringBuilder();
        int bai = n / 100;
        if (bai > 0) {
            sb.append(num1[bai] + " hundred");
        }
        n = n % 100;//十位和个位;
        int shi = n / 10;
        if (shi > 0) {
            if (bai > 0) {
                sb.append(" and ");//有百位加上and
            }
            if (shi == 1) {//如果十位为1,那么十位与个位一起翻译,如:113
                sb.append(num2[n % 10]);
            } else {//否则,十位和个位分别单独翻译,如:123
                sb.append(num3[(shi - 2)] + " ");
                if(n % 10 != 0){
                    sb.append(num1[n % 10]);
                }
            }
        } else if (n % 10 > 0) { //如果没有十位的部分,则直接翻译个位部分,比如:102
            if(bai != 0){
                sb.append(" and ");
            }
            sb.append(num1[n % 10]);
        }
        return sb.toString().trim();
    }
}

发表于 2021-12-31 15:07:17 回复(0)
这题也太坑了,参考大佬们的代码写出符合题目要求的代码如下
import java.util.*;
public class Main {
    static String[] ge = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    static String[] shi = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    static String[] jishi = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            long l = sc.nextLong();
            System.out.println(exchange(l));
        }
    }
    public static String exchange(long num){
        if(num >= 0 && num <= 9){
            return ge[(int)num];
        }
        if(num >= 10 && num <= 19){
            return shi[(int)num % 10];
        }
        if(num >= 20 && num <= 99){
            if(num % 10 == 0){
                return jishi[(int)num / 10 - 2];
            }else{
                return jishi[(int)num / 10 - 2] + " " + ge[(int)num % 10];
            }
        }
        if(num >= 100 && num <= 999){
            if(num % 100 == 0){
                return ge[(int)num / 100] + " hundred";
            }else{
                return ge[(int)num / 100] + " hundred and " + exchange(num % 100); 
            }
        }
        if(num >= 1000 && num <= 999999){
            if(num % 1000 == 0){
                return exchange(num / 1000) + " thousand";
            }
            else{
                return exchange(num / 1000) + " thousand " + exchange(num % 1000);
            }
        }
        if(num >= 1000000 && num <= 999999999){
            if(num % 1000000 == 0){
                return exchange(num / 1000000) + " million";
            }
            else{
                return exchange(num / 1000000) + " million " + exchange(num % 1000000);
            }
        }
        
        return exchange(num / 1000000000) + " billion " + exchange(num % 1000000000);
    }
}
然后呢,我记得hundred thousand 这些单位是有复数形势的,修改一下:
import java.util.*;
public class Main {
    static String[] ge = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    static String[] shi = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    static String[] jishi = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            long l = sc.nextLong();
            System.out.println(exchange(l));
        }
    }
    public static String exchange(long num){
        StringBuilder sb = new StringBuilder("");
        if(num >= 0 && num <= 9){
            sb.append(ge[(int)num]);
            String str =sb.toString();
        return str;
        }
        if(num >= 10 && num <= 19){
            sb.append(shi[(int)num % 10]);
            String str =sb.toString();
        return str;
        }
        if(num >= 20 && num <= 99){
            if(num % 10 == 0){
                sb.append(jishi[(int)num / 10 - 2]);
            }else{
                sb.append(jishi[(int)num / 10 - 2]).append(" ").append(ge[(int)num % 10]);
            }
            String str =sb.toString();
        return str;
        }
        if(num >= 100 && num <= 999){
            if(num >= 100 && num <= 199){
                sb.append(ge[1]).append(" hundred");
            }else{
                sb.append(ge[(int)num / 100]).append(" hundreds");
            }
            if(num % 100 != 0){
                sb.append(" and ").append(exchange(num % 100)); 
            }
            String str =sb.toString();
        return str;
        }
        if(num >= 1000 && num <= 999999){
            if(num >= 1000 && num <= 1999){
                sb.append(ge[1]).append(" thousand");
            }else{
                sb.append(exchange(num / 1000)).append(" thousands");
            }
            if(num % 1000 != 0){
                sb.append(" ").append(exchange(num % 1000));
            }
            String str =sb.toString();
        return str;
        }
        if(num >= 1000000 && num <= 999999999){
            if(num >= 1000000 && num <= 1999999){
                sb.append(ge[1]).append(" million");
            }else{
                sb.append(exchange(num / 1000000)).append(" millions");
            }
            if(num % 1000000 != 0){
                sb.append(" ").append(exchange(num % 1000000));
            }
            String str =sb.toString();
        return str;
        }
        else{
            if(num >= 1000000000 && num <= 1999999999){
                sb.append(ge[1]).append("billion");
            }else{
                sb.append(exchange(num / 1000000000)).append(" billions");
            }
            if(num % 1000000000 != 0){
                sb.append(" ").append(exchange(num % 1000000000));
            }
            String str =sb.toString();
        return str;
        }
    }
}



发表于 2021-12-27 00:01:26 回复(0)