题解 | #学英语#

学英语

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

import java.util.*;

public class Main { public static void main(String[] args) { String[] numbers = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"}; String[] numberWithZero = {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

    String[] powerIndex = {"", "hundred", "thousand", "million", "billion"};

    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()) {
        long inputNumber = sc.nextLong();

        StringBuilder result = new StringBuilder();
        ArrayList<String> lists = new ArrayList<>();
        int power = 1;

        while(inputNumber != 0) {
            if(power != 1) {
                lists.add(powerIndex[power]);
            }

            int t =(int)(inputNumber % 1000); 
            if(t % 100 <= 20) {
                if(t % 100 != 0) {
                    lists.add(numbers[t % 100]);
                }

                if(t / 100 != 0) {
                    if(t % 100 != 0) {
                        lists.add("and");
                    }

                    lists.add("hundred");
                    lists.add(numbers[t / 100]);
                }
            } else {
               if(t % 10 != 0) {
                    lists.add(numbers[t % 10]);
               }

               t /= 10;
               if(t % 10 != 0) {
                    lists.add(numberWithZero[t % 10]);
               }

               t /= 10;
               if(t % 100 != 0) {
                    lists.add("and");
                    lists.add("hundred");
                    lists.add(numbers[t % 100]);
               } 
            }

            inputNumber /= 1000;
            power++;
        }


        for(int i = lists.size() - 1;  i >= 0; i--) {
            if(i != 0) {
                result.append(lists.get(i) + " ");
            } else {
                result.append(lists.get(i));
            }
        }

        System.out.println(result.toString());
    }
}

}

全部评论

相关推荐

11-24 11:23
门头沟学院 C++
点赞 评论 收藏
分享
废铁汽车人:秋招真是牛鬼蛇神齐聚一堂
点赞 评论 收藏
分享
10-09 22:05
666 C++
找到工作就狠狠玩CSGO:报联合国演讲,报电子烟设计与制造
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务