LeetCode第一题,比较简单,这里就不多说了,LeetCode上的解答和代码都比较完善

http://leetcode.com/2012/01/palindrome-number.html

读一下里面的讲解,还是比较有意思的,比如为何不能将字符串倒置,因为signed int的最大是2^32/2-1,首位是2,所以如果是一个20亿多的数,末位比2大的话,一倒置就溢出了



简单写了一下,代码如下,因为程序比较小,也就没有优化风格了

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num;
    printf("please input an number\r\n");
    scanf("%d",&num);
    if(num < 0)
    {
        printf("%d is not palindrome!\r\n",num);
        return 0;
    }
    if(1 == IsPalindrome(num))
    {
        printf("%d is palindrome!\r\n",num);
    }
    else
    {
        printf("%d is not palindrome!\r\n",num);
    }
    return 0;
}
int IsPalindrome(int num)
{
    int div = 1;
    while(num/div > 10) div*=10;
    while(num != 0)
    {
        if((num%10) != (num/div))
        {
            return 0;
        }
        num = (num%div)/10;            //十进制去首位再移位的方法,比较精炼
        div /= 100;
    }
    return 1;
}