华为机试-042-较难-HJ42.学英语


一、描述

Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:

具体规则如下:
1.在英语读法中三位数字看成一整体,后面再加一个计数单位。从最右边往左数,三位一单位,例如12,345 等
2.每三位数后记得带上计数单位 分别是thousand, million, billion.
3.公式:百万以下千以上的数 X thousand X, 10亿以下百万以上的数:X million X thousand X, 10 亿以上的数:X billion X million X thousand X. 每个X分别代表三位数或两位数或一位数。
4.在英式英语中百位数和十位数之间要加and,美式英语中则会省略,我们这个题目采用加上and,百分位为零的话,这道题目我们省略and

下面再看几个数字例句:
22: twenty two
100: one hundred
145: one hundred and forty five
1,234: one thousand two hundred and thirty four
8,088: eight thousand (and) eighty eight (注:这个and可加可不加,这个题目我们选择不加)
486,669: four hundred and eighty six thousand six hundred and sixty nine
1,652,510: one million six hundred and fifty two thousand five hundred and ten

说明:
数字为正整数,不考虑小数,转化结果为英文小写;
保证输入的数据合法
关键字提示:and,billion,million,thousand,hundred。

数据范围:1 ≤ n ≤ 2000000

1.1、输入描述

  • 输入一个long型整数

1.2、输出描述

  • 输出相应的英文写法

二、示例

2.1、示例1

输入:

22

输出:

twenty two

三、答案(java)

3.1、方法一

package com.tzq.hwod;

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

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String[] NUMS = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
				"twenty" };
		String[] NUMSSHI = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
				"ninety" };
		String[] POWER = { "", "hundred", "thousand", "million", "billion" };
		while (in.hasNext()) {
			String line = in.nextLine();
			StringBuilder sb = new StringBuilder();
			ArrayList<String> lists = new ArrayList<>();
			if (!line.matches("\\d+")) {// 如果匹配的不是数字
				System.out.println("error");
			}
			int linenum = Integer.parseInt(line);
			int power = 1;// 单位
			while (linenum != 0) {
				if (power != 1) {
					lists.add(POWER[power]);// 添加单位
				}
				int t = linenum % 1000;// 取低三位
				// 注意小于20,直接读
				if (t % 100 <= 20) {
					if (t % 100 != 0) {// 十位和个位是零的话就不需要读数了
						lists.add(NUMS[t % 100]);
					}
					if (t / 100 != 0) {// 有百位
						if (t % 100 != 0) {// 十位和个位是零的话就不需要添加and了
							lists.add("and");
						}
						lists.add("hundred");
						lists.add(NUMS[t / 100]);
					}
				} else {// 大于20
						// 有个位
					if (t % 10 != 0) {
						lists.add(NUMS[t % 10]);
					}
					t /= 10;
					// 有十位
					if (t % 10 != 0) {
						lists.add(NUMSSHI[t % 10]);
					}
					t /= 10;
					// 有百位
					if (t % 10 != 0) {
						lists.add("and");
						lists.add("hundred");
						lists.add(NUMS[t % 10]);
					}
				}
				linenum /= 1000;// 每次缩小1000倍
				power++;// 单位*1000
			}
			// 添加的时候,先添加低位,读数的时候先读高位,倒着读
			for (int i = lists.size() - 1; i >= 0; i--) {
				if (i != 0) {
					sb.append(lists.get(i) + " ");
				} else {
					sb.append(lists.get(i));// 最后一个不加空格
				}
			}
			System.out.println(sb.toString());
		}
	}
}

在这里插入图片描述

四、答案(python 3)

4.1、方法一

#!/usr/bin/python
# -*- coding: UTF-8 -*-
num1 = ['zero', 'one', 'two', 'three', 'four', 'five', 'six',
        'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
        'thirteen', 'fourteen', 'fifteen', 'sixteen',
        'seventeen', 'eighteen', 'nineteen']
num2 = [0, 0, 'twenty', 'thirty', 'forty', 'fifty', 'sixty',
        'seventy', 'eighty', 'ninety']


# 100以内转英文
def n2w(n):
    if n > 0:
        if n < 20:
            word.append(num1[n])
        else:
            word.append(num2[n // 10])
            if n % 10 != 0:
                word.append(num1[n % 10])


# 1000以内转英文
def hun(n):
    if n >= 100:
        word.append(num1[n // 100])
        word.append('hundred')
        if n % 100 != 0:
            word.append('and')
    n2w(n % 100)


while True:
    try:
        n = int(input())
    except:
        break
    else:
        word = []
        a = n % 1000  # 个十百位
        b = (n // 1000) % 1000  # 个十百千
        c = (n // 1000000) % 1000  # 个十百m
        d = n // 1000000000  # 个十百b

        if d > 0:
            hun(d)
            word.append('billion')
        if c > 0:
            hun(c)
            word.append('million')
        if b > 0:
            hun(b)
            word.append('thousand')
        if a > 0:
            hun(a)
        print(' '.join(word))

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tzq@2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值