#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
bool isPalindrome(int x)
{
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x<0)
return 0;
char c[100];
memset(c, '\0', 100);
sprintf(c, "%d", x);
int i = 0, j = strlen(c)-1;
for(; i<=j; i++, j--)
{
if(c[i] != c[j])
return 0;
}
return 1;
}
int main (int argc, char *argv[])
{
cout<<isPalindrome(100)<<endl;
}
bool isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while (x / div >= 10) {
div *= 10;
}
while (x != 0) {
int l = x / div;
int r = x % 10;
if (l != r) return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
方法一是将整数转化为字符串判断
方法二是直接进行判断 ,负数肯定不是回文
如果输入的直接是字符串 那就更好办了
bool ispalidrome(string str)
{
string::size_type len=str.size();
for(string::size_type i=0;i!=len/2;i++)
{
if(str[i]!=str[len-1-i])
return false;
}
return true;
}
void main()
{
cout<<ispalidrome("1232")<<endl;
}