Input Arabic numerals (integers) and output corresponding Chinese characters (one of the first written questions of Meituan in September 16, 2014)

Source: Internet
Author: User
On September 16, September 16, 2014, Meituan was one of the Nanjing pen test questions. The original requirement is that the number of digits of the input integer can be up to four digits. Here, 12 is extended to the previous level.

Ideas and steps:

1. Identify whether the input is valid and filter the '0' at the top of the string '.

2. Divide the string into four-digit groups. The output mode of each group of four-digit integers is the same. For example, the first four digits and the last four digits of 20402040 are both 2040, both of which output "two thousand and forty", but the first four digits need to be added with the word.

3. 8 ~ 12-digit, 4-digit ~ 8 bits, 0 ~ The four-digit number is output in three cases.

In addition: The shape is 10230401, should read: "One thousand and twenty-threeZeroFour hundred and one ", so after each number is output, check whether the first of the next group is 0, and output '0' as appropriate '.

The following is the implementation code:

Char * arrayofdigit [10] = {"zero", "one", "two", "three", "four", "five", "Six", "Seven ", "Eight", "Nine"}; char * arrayofindex [7] = {"", "", "Ten", "Hundred", "Thousand", "Ten ", ""}; void printgroupof4 (char * pstart, char * pend) {int Index = pend-pstart + 1; // determine the number of digits, add "10," after the number // filter '0' at the top of the string while (pstart <= pend & * pstart = '0 ') {++ pstart; -- index;} // at this time, pstart may "position" and "string format": kilobytes (yxxx), hundreds (0yxx), and ten-digit (00yx), single (000y), // or exit directly (0000), where Y indicates non-zero X indicates an unknown integer while (pstart <= pend & * pstart! = '0') {cout <arrayofdigit [* pstart-'0'] <arrayofindex [Index]; ++ pstart; -- index; If (pstart> pend) {return ;}} // set the flag to determine whether to output '0' bool flag = false; while (pstart <= pend & * pstart = '0 ') {++ pstart; -- index; flag = true;} If (flag = true & pstart <= pend) cout <arrayofdigit [0]; // print the converted string sequence. In this case, pstart can either reach 0 to 0 or directly reach a single position while (pstart <= pend & * pstart! = '0') {cout <arrayofdigit [* pstart-'0'] <arrayofindex [Index]; ++ pstart; -- index; If (pstart> pend) {return ;}} void digittochinese (char * pstr) {If (! Pstr) return; int numberlength = strlen (pstr); // check the symbol bit if (pstr [0] = '-') {cout <"negative"; ++ pstr; -- numberlength;} else if (pstr [0] = '+') {++ pstr; -- numberlength ;} // filter the 0 while (* pstr = '0') before the string {++ pstr; -- numberlength ;} // check whether the subsequent digits are valid for (INT I = 0; I <numberlength; ++ I) {If (pstr [I]> '9' | pstr [I] <'0') {cout <"the input string is invalid! "<Endl; return ;}// if (numberlength = 0) {cout <arrayofdigit [0]; return;} when the input is 0 ;} if (numberlength> 12) {cout <"the input number exceeds the expressed range (hundreds of billions)" <Endl; return ;}// process the first four digits if (numberlength> 8) {printgroupof4 (pstr, pstr + numberlength-9); cout <arrayofindex [6]; If (* (pstr + numberlength-8) = '0 ') // The tens of millions of digits are 0 cout <arrayofdigit [0]; pstr = pstr + numberlength-8; numberlength = 8;} // process the four intermediate digits if (numberlength> 4) {printgroupof4 (pstr, pstr + numberlength-5); cout <arrayofindex [5]; If (* (pstr + numberlength-4) = '0 ') // The thousands are 0 cout <arrayofdigit [0]; pstr = pstr + numberlength-4; numberlength = 4;} // process the last four digits of printgroupof4 (pstr, pstr + numberlength-1);} int main (void) {char P [20]; cout <"enter a number smaller than 12 digits:" <Endl; cin> P; digittochinese (p); Return 0 ;}




Input Arabic numerals (integers) and output corresponding Chinese characters (one of the first written questions of Meituan in September 16, 2014)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.