华为OJ 初级:学英语

描述

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

如22:twenty two,123:one hundred and twenty three。

 

说明:

数字为正整数,长度不超过十位,不考虑小数,转化结果为英文小写;

输出格式为twenty two;

非法数据请返回“error”;

关键字提示:and,billion,million,thousand,hundred。

 

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

 

 

 

知识点 字符串,循环,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归
运行时间限制 10M
内存限制 128
输入

输入一个long型整数

输出

输出相应的英文写法

样例输入 2356
样例输出 two thousand three hundred and fifty six
import java.util.Scanner;

public class Main{
	public static void main(String arg[]) {
		Scanner scan = new Scanner(System.in);
		long num = scan.nextLong();
		scan.close();

		String str = getStr(num);
		System.out.println(str);
	}

	public static String getStr(long num) {
		String str = "";

		String flag = "";
		long l = num / 1000000000;
		if (l != 0) {
			str += getStr(l) + " " + "billion";
//			flag = " ";
		}

		long millon = num / 1000000 % 1000;
		long th = num / 1000 % 1000;
		long h = num % 1000;

		if (millon != 0) {
			str += flag + getHStr(millon) + " " + "million";
			flag = " ";
		}

		if (th != 0) {
			str += flag + getHStr(th) + " " + "thousand";
			flag = " ";
		}

		if (h != 0) {
			str += flag + getHStr(h);
		}
		return str;
	}

	public static String getHStr(long num) {
		String str = "";
		if (num == 0)
			return str;
		int h = (int) (num / 100);
		int t = (int) (num / 10 % 10);
		int g = (int) (num % 10);

		String flag = "";
		if (h != 0) {
			str += flag + name[h] + " " + "hundred";
			flag = " and ";
		}
		if (t != 0) {
			str += flag + name2[t];
			flag = " ";
		}
		if (g != 0) {
			str += flag + name[g];
		}
		return str;
	}

	static String[] name = { "zero", "one", "two", "three", "four", "five",
			"six", "seven", "eight", "nine" };

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值