做了一个关于回文数判断的题目,很简单,但是与预期结果不符合,但又找不到逻辑哪里错了,希望大神告知,谢谢!
#include<iostream>
#include<cmath>
#include "stdlib.h"
using namespace std;
class Solution {
public:
bool isPalindrome(int x) //我的返回类型为布尔型啊,为什么结果却是o或1呢,疑问?
{
int count;
int t[20];
if(x<0)
{
return false;
}
else
{
for(int j=1;;j++)
{
if(x<pow(10,j))
{
count=j;
break;
}
}
for(int k=count-1;k>=0;k--)
{
t[k]=x%10;
x=x/10;
}
for(int i=0;i<count;i++)
{
if(t[i]==t[count-i-1])
return true;
else
return false;
}
}
}
};
void main()
{
Solution s;
cout<<"12为:"<<s.isPalindrome(12)<<endl;
cout<<"121为:"<<s.isPalindrome(121)<<endl;
cout<<"12221为:"<<s.isPalindrome(12221)<<endl;//结果为0就代表错误,也即非回文数,反之为1就代表该数为回文数
cout<<"1221为:"<<s.isPalindrome(1221)<<endl;
}