题目:回文数

因为负数不可能为回文数,所以直接排除

之后就和之前的题目一样,把反转后的数字再与原数字比较就行

(注意:这里使用int会报错–超出了类型范围,所以改用了unsigned int,因为负数在第一关就被筛掉了)

isPalindrome()

 1bool isPalindrome(int x)
 2    {
 3        if(x < 0) return false;
 4        unsigned int result = 0, x_copy = x;
 5        while(x != 0)
 6        {
 7            unsigned int a = x % 10;
 8            x /= 10;
 9            result = result * 10 + a;
10        }
11        if(result == x_copy)
12            return true;
13        return false;
14    }

来源:语雀原笔记