9. Palindrome Number
问题:
Determine whether an integer is a palindrome. Do this without extra space.
解析:
什么是回文数?
答案:“回文”是指正读反读都能读通的句子,它是古今中外都有的一种修辞方式和文字游戏,如“我为人人,人人为我”等。在数学中也有这样一类数字有这样的特征,成为回文数(palindrome number)。设n是一任意自然数。若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数。例如,若n=1234321,则称n为一回文数;但若n=1234567,则n不是回文数。
判断一个int型数是不是回文数,而且不能用额外空间。
注意:负数没有回文数,因为不知道这个东西,折腾半天。
#include<stdio.h>
#include < limits.h >
#include < float.h >
void main()
{
bool m=false;
int x=0;
bool isPalindrome(int x);
printf("Please give a data:");
scanf("%d",&x);
m=isPalindrome(x);
if(m== true)
printf("The Number is Palindrome Number.");
else
printf("The Number is not Palindrome Number.");
}
bool isPalindrome(int x) {
int reg=0;
int temp = x;
int flag = 0;
if (x < 0)
{
return false;
}
if(x > INT_MAX || x < INT_MIN)
{
x = 0;
}
while(x)
{
reg=reg*10+x%10;
x=x/10;
}
if(temp == reg) // 注意这个地方 以前用的X是不对的,因为while循环时候 已经把x进行改变
{
return true;
}
else
{
return false;
}
}