題目:
將非負整數轉換爲其對應的英文表示。可以保證給定輸入小於 231 - 1 。
示例 1:
輸入: 123
輸出: "One Hundred Twenty Three"
示例 2:
輸入: 12345
輸出: "Twelve Thousand Three Hundred Forty Five"
示例 3:
輸入: 1234567
輸出: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
示例 4:
輸入: 1234567891
輸出: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
題解:
- 非負數包含了0。
- 231 - 1 表明在int類型的數據範圍之內。
- 數字翻譯成英文時候,一般3個數爲一組,單位分別爲千、百萬、十億。
- 轉換時候注意每次將一個三位數轉換成爲英文,然後加上3中的單位。
代碼:
public class EnglishTrans{
public static void main(String[] args) {
// 2134569087
// 2100000000
int num = 2134569087;
String str = numberToWords(num);
System.out.println("***********************************************************************************************");
System.out.println("翻譯:" + str);
System.out.println("***********************************************************************************************");
}
public static String numberToWords(int num) {
if (num == 0) {
return "Zero";
}
// 循環次數標記
int flag = 0;
StringBuilder builder = new StringBuilder();
while (num > 0) {
int temp = num % 1000;
// 爲0時候不做計算
if (temp != 0) {
if (flag == 0) {
// 將三位數轉換成爲英文,在加上單位
builder.append(transLowThousandToEnglish(temp));
} else {
StringBuilder sb = new StringBuilder();
StringBuilder append = sb.append(transLowThousandToEnglish(temp)).append(" ").append(map().get((int) Math.pow(1000, flag))).append(" ");
// 因爲是從個位算起,故將後得到的翻譯置於首位
builder.insert(0, append);
}
}
flag++;
// 翻譯目前最低的三位後,捨棄後三位
num /= 1000;
}
return builder.toString().trim();
}
/**
* 構建中英文對應的map
*/
private static Map<Integer, String> map() {
Map<Integer, String> map = new HashMap<>(32);
map.put(0, "Zero");
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
map.put(4, "Four");
map.put(5, "Five");
map.put(6, "Six");
map.put(7, "Seven");
map.put(8, "Eight");
map.put(9, "Nine");
map.put(10, "Ten");
map.put(11, "Eleven");
map.put(12, "Twelve");
map.put(13, "Thirteen");
map.put(14, "Fourteen");
map.put(15, "Fifteen");
map.put(16, "Sixteen");
map.put(17, "Seventeen");
map.put(18, "Eighteen");
map.put(19, "Nineteen");
map.put(20, "Twenty");
map.put(30, "Thirty");
map.put(40, "Forty");
map.put(50, "Fifty");
map.put(60, "Sixty");
map.put(70, "Seventy");
map.put(80, "Eighty");
map.put(90, "Ninety");
map.put(100, "Hundred");
// 千,百萬,十億
map.put(1000, "Thousand");
map.put(1000000, "Million");
map.put(1000000000, "Billion");
return map;
}
/**
* 將一個三位數轉化爲英文,因爲這個三位數可能在比較大的數的中間:如(1,000,369,124)中的369,所以爲0時不能返回zero
*/
private static String transLowThousandToEnglish(int num) {
// 不能返回Zero
if (num == 0) {
return "";
}
// 順序從小到大比較好
if (num <= 20) {
return map().get(num);
} else if (num > 20 && num < 100) {
StringBuilder sb = new StringBuilder();
// 十位數
sb.append(map().get(num / 10 * 10));
// 個位數
int units = num % 10;
if (units != 0) {
sb.append(" ").append(map().get(units));
}
return sb.toString();
} else if (num >= 100 && num < 1000) {
StringBuilder sb = new StringBuilder();
// 百位數
sb.append(map().get(num / 100)).append(" ").append("Hundred");
int tensDigit = num % 100;
// 十位數
if (tensDigit > 0 && tensDigit <= 20) {
sb.append(" ").append(map().get(tensDigit));
} else if (tensDigit > 20 && tensDigit < 100) {
sb.append(" ").append(map().get(tensDigit / 10 * 10));
int units = tensDigit % 10;
// 個位數
if (units > 0) {
sb.append(" ").append(map().get(units));
}
}
return sb.toString();
} else {
return "ERROR";
}
}
}