【华为OJ】【092-学英语】

105 篇文章 155 订阅

【华为OJ】【算法总篇章】


【华为OJ】【092-学英语】

【工程下载】


题目描述

Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
如22:twenty two,123:one hundred and twenty three。

说明
数字为正整数,长度不超过十位,不考虑小数,转化结果为英文小写;
输出格式为twenty two;
非法数据请返回“error”;
关键字提示:and,billion,million,thousand,hundred。

方法原型:public static String parse(long num)

输入描述:

输入一个long型整数

输出描述:

输出相应的英文写法

输入例子:

2356

输出例子:

two thousand three hundred and fifty six

算法实现

import java.util.Scanner;

/**
 * Author: 王俊超
 * Date: 2016-05-03 08:37
 * CSDN: http://blog.csdn.net/derrantcm
 * Github: https://github.com/Wang-Jun-Chao
 * Declaration: All Rights Reserved !!!
 */
public class Main {

    private final static String WORDS[] = {
            "", "one", "two", "three", "four", "five",// 1-5
            "six", "seven", "eight", "nine", "ten",// 6-10
            "eleven", "twelve", "thirteen", "fourteen", "fifteen",// 11-15
            "sixteen", "seventeen", "eighteen", "nineteen", "twenty",// 16-20
            "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"// 30-90
    };

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNextLong()) {
            long num = scanner.nextLong();
            System.out.println(parse(num));
        }
        scanner.close();
    }

    private static String parse(long num) {
        if (num <= 0) {
            return "error";
        } else if (num < 20) {  // 20以下
            return WORDS[(int) num];
        } else if (num < 100) {
            if (num % 10 == 0) {
                return WORDS[(int) (20 + (num - 20) / 10)];     // 20 30 40 50 60 70 80 90
            } else {
                return parse(num / 10 * 10) + " " + parse(num % 10);
            }
        } else if (num < 1_000) { // 千以下
            if (num % 100 == 0) {
                return parse(num / 100) + " hundred";
            } else {
                return parse(num / 100) + " hundred and " + parse(num % 100);
            }
        } else if (num < 1_000_000) { // 百万以下
            if (num % 1_000 == 0) {
                return parse(num / 1_000) + " thousand";
            } else {
                return parse(num / 1_000) + " thousand " + parse(num % 1_000);
            }
        } else if (num < 1_000_000_000) { // 亿
            if (num % 1_000_000 == 0) {
                return parse(num / 1_000_000) + " million";
            } else {
                return parse(num / 1_000_000) + " million " + parse(num % 1_000_000);
            }
        } else if (num < 10_000_000_000L) { // 十亿
            if (num % 1_000_000_000 == 0) {
                return parse(num / 1_000_000_000) + " billion";
            } else {
                return parse(num / 1_000_000_000) + " billion " + parse(num % 1_000_000_000);
            }
        } else {
            return "error";
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值