Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Moderate 整数打印读法 @CareerCup

$
0
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);
		}
	}
}


作者:hellobinfeng 发表于2013-12-3 2:16:05 原文链接
阅读:125 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>