废话不多说,直接上代码;
int Reverse(int k ,int f)//逆置函数
{
int m = 0;
while (f--)
{
m += (k % 10)* pow(10, f);
k = k / 10;
}
return m;
}
void Symmetry(int a)//判断函数
{
int count = 1;
int tmp = a;
while (tmp = tmp / 10)
{
count++;
}
if (count % 2 == 0)
{
int k = pow(10, count / 2);
int high = a / k;
int low = a % k;
int f = count / 2;
int ret = Reverse(low,f);
if (ret == high)
{
printf("%d是回文数\n", a);
}
else
{
printf("%d不是回文数\n", a);
}
}
else
{
int k = pow(10, count / 2);
int high = a / k;
int low = a % k;;
int f = count / 2;
int l = pow(10, f);
int ret = Reverse(high, f+1);
if (ret%l == low)
{
printf("%d是回文数\n", a);
}
else
{
printf("%d不是回文数\n", a);
}
}
}
int main()
{
int a = 1235321;
Symmetry(a);
system("pause");
return 0;
}
思路:
1:把数字分为两部分;比如,将123321分为123和321
2:逆置一个
3:比较,如果相等就是回文数
注意:位数是单数的要另算;比如12321;