这题上次回家微策略的电面题,也是当时写代码,当时思路混乱没立刻写出来,后来好像说了说想法,想法是对的。忘了是不是当初死在这题上了,现在都无所谓了。
#include "stdafx.h"
#include <iostream>
using namespace std;
bool isPalindrome(int x) {
if (x<0)
{
return false;
}
int highbit=1;
int temp = x;
temp = temp/10;
if (temp==0)
{
return true;
}
while(temp!=0)
{
highbit*=10;
temp/=10;
}
const int lowbit=10;
while(highbit>=lowbit)
{
if (x/highbit!=x%lowbit)
{
return false;
}
x%=highbit;//delete the highest bit
x/=lowbit;//delete the lowest bit
highbit/=100;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a;
while(cin>>a)
{
cout<<isPalindrome(a)<<endl;
}
system("pause");
return 0;
}