The judgement of Palindrome number (three methods)

Source: Internet
Author: User

Recently done a little about palindrome number of the summary.
First of all, write a number of methods for the determination of palindrome numbers.
The concept of palindrome numbers: that is, given a number, this number is the same as the read and reverse. For example: 121,1221 is a palindrome number, 123,1231 is not a palindrome number.
method One:
Try out situations and deal with small numbers. Use mathematical methods. The number of palindrome numbers entered is x<10^9,int storage, or the number of X<10^18,long long stored, the number of the range is not large. Here is an int storage case.

#include <stdio.h>
int main ()
{
    int x,newed,t,n;
    while (scanf ("%d", &x)!=eof)
    {
        newed=0;
        n=x;
        Do
        {
            newed=newed*10+x%10;
            x/=10;
        } while (x>0);
        if (n==newed)
            printf ("yes\n");
        else
            printf ("no\n");
    }
    return 0;
}

Method Two:
Try out scenarios and handle large numbers. Use string handling. Because palindrome number about center symmetry, as long as the more symmetrical number can.

#include <stdio.h>
#include <string.h>
int main ()
{
    int i,length,flag=1;
    Char a[100];
    Gets (a);
    Length=strlen (a);
    for (i=0;i<=length/2;i++) {
       if (A[i]!=a[length-i-1]) {
           flag=0;
           break;
       }
    }
    if (flag==1)
      printf ("yes");
    else
      printf ("no");
    return 0;
}

Method Three:
Try out scenarios and handle large numbers. The idea of using stacks. Similar to string handling, here is a comparison of stack elements with string characters, such as unequal, is no.

 #include <stdio.h> #include <string.h> #define STACKSIZE typedef struct {Char
    data[stacksize];//open stack for 100;
int top=0;
}seqstack;
    int main () {seqstack s;
    Char str[100];
    scanf ("%s", str);
    int Len=strlen (str);
    int i,flag=1;
    for (i=0;i<len/2;i++)//will be half the characters into the stack s.data[s.top++]=str[i]; if (len%2) i++;
    Odd-numbered automatically skip the middle of the number, such as 121, skip 2, compare 1 while (s.top)//equivalent to Emptystack (&s), to determine whether the stack empty.
        {///each popup character compares char temp=s.data[--s.top with corresponding character];
        if (Temp!=str[i]) {flag=0;
        }else{i++;
    } if (flag==0) {printf ("no\n");
    }else{printf ("yes\n");
return 0; }

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.