[Problem description]
Enter a positive integer n (n is a 4-digit integer at the maximum) and output its English expression.
[Example]
Input: 1
Output: One
Input: 12
Output: twelve
Right input: 135
Output: one hundred thirty five
Idea: 1. First, a number less than 19 can be output directly ..
2. 20 ~~~ Numbers smaller than 99. Integers of up to 10 can be directly output. Otherwise, divide by 10 to output ten digits, and return one digit from 10 modulo output.
3. 100 ~~~ Divide the number within 999 and divide it by 100 to output a hundred-digit number. Then, use the modulo value of 100 to get a two-digit number and convert it to 2.
4. 1000 ~~~ Divide the number within 9999 and divide it by 1000 to output a thousand-digit number. After modulo with 1000, a three-digit number is obtained, which is converted to 3.
The complete implementation code is as follows:
# Include "iostream" using namespace STD; char table [20] [20] = {"zero", "one", "two", "three", "four ", "five", "Six", "Seven", "eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen ", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "ninteen"}; char tens [12] [20] = {"", "Ten ", "Twenty", "Thirty", "Forty", "ty", "Sixty", "Seventy", "Eighty", "ninty", "Hundred ", "Thousand"}; void print (int n) {If (n> = 0 & n <= 19) {cout <Table [N]; // directly output numbers less than 19} else if (n> = 20 & n <= 99 & N % 10 = 0) // The entire 10 {cout <tens [N/10];} else if (n> = 20 & n <= 99) // output ten first, output a single location {cout <tens [N/10] <"" <Table [n % 10];} else if (n> = 100 & n <= 999) {print (N/100); cout <"<tens [10] <""; // output hundred-bit print (N % 100); // recursive call, output ten digits and one digit} else if (n> = 1000 & n <= 999999) {print (N/1000); cout <"" <tens [11] <""; // outputs a thousand-bit print (N % 1000); // recursive call, output bits, 10 bits, and 1 bits} int main (void) {int N; while (CIN >>> N) {print (n); cout <Endl ;} system ("pause"); Return 0 ;}
Run the following command: