Moderate 整数打印读法 @CareerCup

1246人阅读 评论(0) 收藏 举报
分类:

比较繁琐,要处理的case很多



package Moderate;

import CtCILibrary.AssortedMethods;

/**
 * Given  any  integer, print  an  English  phrase  that  describes  the  integer  (e.g., "One
Thousand, Two Hundred Thirty  Four").

给定一个整数,打印出它的读法
如1234 打印 "One Thousand, Two Hundred Thirty  Four"
 *
 */

public class S17_7 {
	public static String[] digits = { "One", "Two", "Three", "Four", "Five",
			"Six", "Seven", "Eight", "Nine" };
	public static String[] teens = { "Eleven", "Twelve", "Thirteen",
			"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
			"Nineteen" };
	public static String[] tens = { "Ten", "Twenty", "Thirty", "Forty",
			"Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
	public static String[] bigs = { "", "Thousand", "Million" };

	public static String numToString(int number) {
		if (number == 0) {
			return "Zero";
		}

		if (number < 0) {
			return "Negative " + numToString(-1 * number);
		}
		int count = 0;
		String str = "";

		while (number > 0) {
			if (number % 1000 != 0) {
				str = numToString100(number % 1000) + bigs[count] + " " + str;
			}
			number /= 1000;
			count++;
		}

		return str;
	}

	public static String numToString100(int number) {
		String str = "";

		/* Convert hundreds place */
		if (number >= 100) {
			str += digits[number / 100 - 1] + " Hundred ";
			number %= 100;
		}

		/* Convert tens place */
		if (number >= 11 && number <= 19) {
			return str + teens[number - 11] + " ";
		} else if (number == 10 || number >= 20) {
			str += tens[number / 10 - 1] + " ";
			number %= 10;
		}

		/* Convert ones place */
		if (number >= 1 && number <= 9) {
			str += digits[number - 1] + " ";
		}

		return str;
	}

	public static void main(String[] args) {
		/* numbers between 100000 and 1000000 */
		for (int i = 0; i < 8; i++) {
			int value = (int) Math.pow(10, i);
			String s = numToString(-1 * value);
			System.out.println(-value + ": " + s);
		}
		

		/* numbers between 0 and 100 */
		for (int i = 0; i < 10; i++) {
			int value = AssortedMethods.randomIntInRange(0, 100);
			String s = numToString(value);
			System.out.println(value + ": " + s);
		}

		/* numbers between 100 and 1000 */
		for (int i = 0; i < 10; i++) {
			int value = AssortedMethods.randomIntInRange(100, 1000);
			String s = numToString(value);
			System.out.println(value + ": " + s);
		}

		/* numbers between 1000 and 100000 */
		for (int i = 0; i < 10; i++) {
			int value = AssortedMethods.randomIntInRange(1000, 100000);
			String s = numToString(value);
			System.out.println(value + ": " + s);
		}

		/* numbers between 100000 and 1000000 */
		for (int i = 0; i < 10; i++) {
			int value = AssortedMethods.randomIntInRange(100000, 100000000);
			String s = numToString(value);
//			System.out.println(value + ": " + s);
		}

		/* numbers between 100000 and 1000000 */
		for (int i = 0; i < 10; i++) {
			int value = AssortedMethods.randomIntInRange(100000000, 1000000000);
			String s = numToString(value);
//			System.out.println(value + ": " + s);
		}
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    博客专栏
    个人资料
    • 访问:1223183次
    • 积分:16571
    • 等级:
    • 排名:第606名
    • 原创:407篇
    • 转载:64篇
    • 译文:0篇
    • 评论:291条