Facebook Interview - Integer to English Phrase - 我的博客 - ITeye技术网站
Given an integer between 0 and 999,999, print an English phrase that describes the integer (eg, "One Thousand, Two Hundred and Thirty Four")
http://www.dzone.com/snippets/convert-integer-english-0
"Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tens ={"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
String[] bigs ={"", "Thousand", "Million", "Billion"};
String hundred= "Hundred";
String negative = "Negative";
String convert(int num) {
if (num == 0) {
return smalls[0];
} else if (num < 0) {
return negative + " " + convert( -1 * num);
}
Linkedlist<String> parts = new Linkedlist<String>();
int chunkCount = 0;
while (num > 0) {
if (num % 1000 != 0) {
String chunk= convertChunk(num % 1000) + " " + bigs[chunkCount];
parts.addFirst(chunk);
}
num /= 1000; // shift chunk
chunkCount++;
}
return listToString(parts);
}
String convertChunk(int number) {
Linkedlist<String> parts = new Linkedlist<String>();
/* Convert hundreds place */
if (number>= 100) {
parts.addlast(smalls[number / 100]);
parts.addLast(hundred);
number%= 100;
}
/* Convert tens place */
if (number >= 10 && number <= 19) {
parts.addlast(smalls[number]);
} else if (number >= 20) {
parts.addlast(tens[number / 10]);
number%= 10;
}
/* Convert ones place */
if (number >= 1 && number <= 9) {
parts.addlast(smalls[number]);
}
return listToString(parts);
}
/* Convert a linked list of strings to a string, dividing it up with spaces. */
String listToString(LinkedList<String> parts) {
StringBuilder sb = new StringBuilder();
while (parts.size() > 1) {
sb.append(parts.pop());
sb.append(" ");
}
sb.append(parts.pop());
return sb.toString();
}
Read full article from Facebook Interview - Integer to English Phrase - 我的博客 - ITeye技术网站
Given an integer between 0 and 999,999, print an English phrase that describes the integer (eg, "One Thousand, Two Hundred and Thirty Four")
- vector<string> under20 = {"Zero", "One", "Two", "Three", "Four", "Five",
- "Six", "Seven", "Eight", "Nine", "Ten",
- "Eleven","Twelve", "Thirteen", "Fourteen", "Fifteen",
- "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"};
- vector<string> tens = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
- "Sixty", "Seventy", "Eighty", "Ninety"};
- string numBelow1000(int num) {
- if(num == 0) return under20[0];
- string ret = "";
- if (num > 100) {
- ret = under20[num/100] + " Hundred";
- num %= 100;
- if (num == 0) {
- return ret;
- } else {
- ret += " and ";
- }
- }
- if (num <= 20) {
- ret += under20[num];
- } else {
- ret += tens[num/10];
- num %= 10;
- if (num > 0)
- ret += " " + under20[num];
- }
- return ret;
- }
- string num2string(int num) {
- int part1 = num / 1000;
- int part2 = num % 1000;
- string res;
- if(part1) {
- res = numBelow1000(part1) + " Thousand";
- if(part2) res += ", " + numBelow1000(part2);
- } else {
- res = numBelow1000(part2);
- }
- return res;
- }
http://www.dzone.com/snippets/convert-integer-english-0
Given any integer, print an English phrase that describes the integer (e.g., "One Thousand, Two Hundred Thirty Four").
String[] smalls = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven","Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tens ={"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
String[] bigs ={"", "Thousand", "Million", "Billion"};
String hundred= "Hundred";
String negative = "Negative";
String convert(int num) {
if (num == 0) {
return smalls[0];
} else if (num < 0) {
return negative + " " + convert( -1 * num);
}
Linkedlist<String> parts = new Linkedlist<String>();
int chunkCount = 0;
while (num > 0) {
if (num % 1000 != 0) {
String chunk= convertChunk(num % 1000) + " " + bigs[chunkCount];
parts.addFirst(chunk);
}
num /= 1000; // shift chunk
chunkCount++;
}
return listToString(parts);
}
String convertChunk(int number) {
Linkedlist<String> parts = new Linkedlist<String>();
/* Convert hundreds place */
if (number>= 100) {
parts.addlast(smalls[number / 100]);
parts.addLast(hundred);
number%= 100;
}
/* Convert tens place */
if (number >= 10 && number <= 19) {
parts.addlast(smalls[number]);
} else if (number >= 20) {
parts.addlast(tens[number / 10]);
number%= 10;
}
/* Convert ones place */
if (number >= 1 && number <= 9) {
parts.addlast(smalls[number]);
}
return listToString(parts);
}
/* Convert a linked list of strings to a string, dividing it up with spaces. */
String listToString(LinkedList<String> parts) {
StringBuilder sb = new StringBuilder();
while (parts.size() > 1) {
sb.append(parts.pop());
sb.append(" ");
}
sb.append(parts.pop());
return sb.toString();
}
Read full article from Facebook Interview - Integer to English Phrase - 我的博客 - ITeye技术网站