一道网传的亚马逊笔试题解法

    闲来无事,就在51CTO上看Java设计模式,结果碰巧看到一道据传是亚马逊的笔试题,然后就想试试,由于个人的能力有限,就写了如下的结果,望批评指正。题目如下:

    /**Given an integer between 0 – 999,999, print an English sentence that
     * describes the integer, for example, input 10, the output is ten
     * Note:
     * Each word should be lower case, for example, one hundred and twenty,
     * and there should be one and only one space between each word
     * Samples:
     * input: 1 output: one
     * input: 23 output: twenty three
     * input: 105 output: one hundred and five
     * input: 12345 output: twelve thousand three hundred and forty five
     * input: 123456 output: one hundred and twenty three thousand four hundred and fifty six
     * Please complement the printNumber method in Solution class:
     */

    然后他提供的代码是:

    

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        int num = Integer.parseInt(br.readLine());
        printNumber(num);
        isr.close();
        br.close();
    }
    private static void printNumber(int num) {
    }
}

    要求在函数里把解法写上去。本来是想用递归的,结果感觉必须把结果写在函数里,然后就写了,基本的思路就是把数字分为1000以上和1000以下,然后分别判断。具体的代码如下:

private static void printNumber(int num) {
	String Num_String = "", num1_s = "", num2_s = "";
	String[] nums = {"", "one", "two", "three", "four", "five", "six", "seven",
			"eight", "nine", "ten", "eleven", "twelve", "thirteen", "forteen",
			"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
	String[] tenNums = {"ten","twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
	if(num == 0){
		System.out.println("zero");
		return;
	}
	//----------------------------读后3位
	int num2 = num % 1000;
	if(num2 != 0){
		if(num2/100 != 0 && num2%100!=0){//十位有数字
			num2_s = nums[num2 / 100] + " hundred ";
		}else if(num2/100 != 0 && num2%100==0 && num > 900){//后面没有数字
			num2_s = "and " + nums[num2 / 100] + " hundred ";
		}else if (num2/100 != 0 && num2%100==0) {
			num2_s = nums[num2 / 100] + " hundred ";
		}
		if (num2%100 != 0) {//说明低两位有数字
			if (num2 / 100!=0 || num > 900) {
				num2_s = num2_s +"and ";
			}
			if (num2 % 100 >= 10) {//说明十位有数字
				if(num2 % 100 <= 19){//如果是10~19
					num2_s = num2_s + nums[10 + num2%10];
				}else {//说明大于等于20
					num2_s = num2_s + tenNums[(num2-100*(num2/100))/10-1] + " " + nums[num2%10];
				}
			}else {//说明只有个位有数字
				num2_s = num2_s + nums[num2%10];
			}
		}
	}
	//----------------------------读前3位
	int num1 = num / 1000;
	if(num1 != 0){
		if(num1/100 != 0){
			num1_s = nums[num1 / 100] + " hundred ";
		}
		if (num1%100 != 0) {//说明低两位有数字
			if (num1 / 100 != 0) {
				num1_s = num1_s +"and ";
			}
			if (num1 % 100 >= 10) {//说明十位有数字
				if(num1 % 100 <= 19){//如果是10~19
					num1_s = num1_s + nums[10 + num1%10];
				}else {//说明大于等于20
					num1_s = num1_s + tenNums[(num1-100*(num1/100))/10-1] + " " + nums[num1%10];
				}
			}else {//说明只有个位有数字
				num1_s = num1_s + nums[num1%10];
			}
		}
		num1_s = num1_s + " thousand ";
	}
        Num_String = num1_s + num2_s;
	System.out.println(Num_String.replaceAll(" ", ""));
}

    网上那位网友的解法和我的不一样,但是效果一样,他用到了递归,然后代码也比我的简洁一点,现在把他的地址贴出来,大家也可以参考下。请猛戳这里

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.