9. Palindrome number

Source: Internet
Author: User

The article directory is as follows

(1) Your own ideas

(2) your own code

(3) Other people's ideas

(4) Someone else's code

(5) Compare your shortcomings

(1) Your own ideas

Also met palindrome number, but this time is a lot simpler, because this time just to determine whether a number x is a palindrome number, then only need to reverse an integer, and then save as a new number R, and finally compare the X and r are equal. During the period involved in the remainder, the division operation must have read the code will understand, not many, directly paste code

(2) your own code

classSolution { Public:    BOOLIspalindrome (intx) {intTMPX =x; intTMP =0;  while(tmpx!=0) {tmp= tmp*Ten+ (tmpx%Ten); TMPX= tmpx/Ten; }                return(tmp==x); }};

(3) Other people's ideas

Each time the highest and lowest digits of the integer x are compared, and then the X is disassembled, the highest and lowest bits of x are removed, and then the comparison is continued, and if one is not satisfied, then it is not a palindrome number, otherwise it will be returned to the number of palindrome until the comparison is over.

(4) Someone else's code

classSolution { Public:    BOOLIspalindrome (intx) {if(X <0)return false; intD =1;//Divisor         while(x/d >=Ten) d *=Ten;  while(X >0) {            intQ = x/d;//Quotient            intr = x%Ten;//remainder            if(q! = r)return false; X= x% d/Ten; D/= -; }        return true; }};

(5) Compare your shortcomings

I constructed another number r based on the number x, where r is in the opposite order of X. Other people's thoughts only operate on X, and the beginning of a comparison number is equal to the number at the end.

One of my methods has a fatal disadvantage, that is, when the integer is particularly large (beyond the scope of int), in the process of constructing r is prone to error, but other people's methods do not have this problem, so again encountered this problem, try to operate on the same number, so as to avoid the problem of numerical range!

9. Palindrome number

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.