"Cracking The Coding interview programmer interview Gold Code"----Digital Pronunciation _ Programming Basics

Source: Internet
Author: User
Time limit: 3 seconds Space limit: 32768K heat index: 325 subject Knowledge Point: Programming basic Algorithm Knowledge Video explanation topic description

There is a non-negative integer, write an algorithm that prints the English description of the integer.

Given an int x, return a string that describes the integer in English. Test Sample:

1234

Back: "One thousand,two hundred thirty Four"

Train of thought: This problem is not difficult, but very troublesome, want 1.1 points to think clearly can write

The code is as follows:

Class ToString {public:string tostring (int x) {string base[20] = {"", "one", "two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",                              "Eleven", "Twelve", " Thirteen "," fourteen "," fifteen "," Sixteen "," Seventeen ",                                "Eighteen", "Nineteen"}; //0~19         string tyNum[10] = {"", "
"," Twenty "," Thirty "," Forty "," Fifty "," Sixty "," Seventy "," Eighty "," Ninety "};
        string Bignum[4] = {"", "Thousand", "Million", "Billion"};
        string Res;
        int k=0;         //The X per 3-digit number into a group for encoding         while (x>0) {
            int val = x%1000;             //encodes every 3-digit number     
        string temp;             if (base[val/100]!= "") {  
              temp+=base[val/100];                 temp+= "Hundred
";             }              if (VAL%100<20) { //number less than 20 separate code       
          if (!temp.empty ()) temp+= "";                temp+=base[val
%100];             }else{     
           if (!temp.empty ()) temp+= "";                 temp+=tynum[
VAL%100/10];                 if (base[val%10 ]!= "") {                 
   if (!temp.empty ()) temp+= "";                   
  temp+=base[val%10];                 }             &NBSP}             if (!temp.empty () &&bignum[k]!= "") {              
  temp+= "";                 temp+=bignum[k
];                 if (!res.empty (
) temp+= ",";             }       
      res = Temp+res;
            k++;
            x/= 1000;
           &NBSP}         return Res; }
};

Do not understand can add my QQ group: 261035036 (IT programmer interview Treasure

Group) Welcome your arrival Oh, see the blog to point Footprints Bai, thank you ~ ~

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.