1. Title
Integer to Chinese Words (output numbers corresponding to words by English reading)
2. Address of the topic
https://leetcode.com/problems/integer-to-english-words/
3. Topic content
English: Convert a non-negative integer to its 中文版 words representation. Given input is guaranteed to being less than 231-1.
Chinese: Give a non-negative integer, output the number in English corresponding to the word, the number is less than 2^31-1
For example: The following is an example of a three conversion
123, "One hundred twenty three" 12345, twelve thousand three hundred forty Five "1234567" one Million-H Undred Thirty four thousand Five hundred sixty Seven "
4. Methods of Solving problems
In fact, I wanted to complicate the subject at first. This problem does not need to consider the English intermediate place to write "and" situation. For example, 101, in English The Reading method is "one hundred and a", in this problem, only need to output "a hundred". Therefore, to AC the subject only need to be the corresponding position of the number and digital write pairs on it. The Java code is as follows:
/** * @ function Description: leetcode 273 - integer to english words * @ Developer: tsybius2014 * @ Development Date: October 12, 2015 */public class Solution { /** * output Arabic numerals according to the English reading method * @param num numbers, range:[0,2^31-1] * @return */ public string numbertowords (Int num) { if (num < 0) { return ""; } //number is 0 direct return if (num == 0) { return "Zero"; } //left paragraph int segment1 = num / 1000000000; //paragraph 1:1 billion-千亿位 int segment2 = num % 1000000000 / 1000000; //paragraph 2: Million dollar-billion-bit int segment3 = num % 1000000 / 1000; //paragraph 3: thousand-100,000 bits int segment4 = num % 1000; //paragraph 4: bits-hundred String result = ""; if (segment1 > 0) { result += numtowordslessthan1000 (segment1) + " " + "billion"; } if (segment2 > 0) { result += numtowordslessthan1000 (Segment2) + " " + " Million "; } if (segment3 > 0) { result += numtowordslessthan1000 (SEGMENT3) + " " + "thousand"; } if (segment4 > 0) &NBSP;{&NBSP;&NBsp; result += numtowordslessthan1000 ( SEGMENT4); } Return result.trim (); } /** * output Arabic numerals according to English reading * @param num numbers Scope: (0,1000) * @return */ private string numtowordslessthan1000 (Int num) { if (num == 0 | | num >= 1000) { return ""; } string result = ""; if (num >=&NBSP;100) { result += numtowordsbase (num / 100) + " " + "hundred"; } num = num % 100; if (num > 20) { result += numtowordsbase (num / 10 * &NBSP;10); if (num % 10 != 0) { result += numtowordsbase (num % 10); } } else if (num > 0) { result += Numtowordsbase (num); } return result; } /** * output Arabic numerals corresponding to the word * by English reading method @param num numbers, Range: (0,20) ∪{30,40,50,60,70,80,90} * @return */ private string numtowordsbase (Int num) { String result = " "; switch (num) { case 1: result += "onE "; break; case 2: result += "; break; case 3: result += "three"; break; case 4: result += "Four"; break; case 5: result += "Five"; break; case 6: result += "Six"; break; case 7: result += "Seven"; break; case 8: result += "Eight"; break; case 9: result += "Nine"; break ; case 10: result += "Ten"; break; case 11: result += "Eleven"; break; case 12: result += "Twelve"; break; case 13: result += "Thirteen"; break; case 14: result += "Fourteen"; break; case 15: result += "Fifteen"; break; case 16: result += "Sixteen"; break; case 17: result += "Seventeen"; break; case 18: result += "Eighteen"; break; case 19: result += "Nineteen"; break; case 20: result += "Twenty"; break; case 30: result += "Thirty"; break; case 40: result += "Forty"; break; case 50: result += "Fifty"; break; case 60: result += "Sixty"; break; case 70: result += "Seventy"; break; case 80: result += "Eighty"; break; case 90: result += "Ninety"; break; } return result; }}
END
Leetcode:integer to Chinese Words-output numbers by English reading corresponding words