所谓“回文数”是指读一个自然数,从正方向读和反方向读,结果是一样的。例如:646,1551,891232198都认为是回文数。
常规方法是把数的各位放入数组中,int i[];从i[0]到i[max]的数等于从i[max]到i[0]的数。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2010-05-19
#include <math.h>
unsigned GetBit(unsigned num, int bit)
{
if (bit < 1) return 0;
return (unsigned)((num-((int)(num/pow((float)10,bit))) *pow((float)10,bit))/pow((float)10, bit-1)) ;
}
void main()
{
int num,len,i;
while(scanf("%d", &num))
{
len = 0;
if (!num) break;
i = num;
while( i > 0)
{
i = i/10;
len++;
}
for(i=len; i>len/2; i--)
{
if ( GetBit(num,i) != GetBit(num,len-i+1) )
{
printf("No\n");
i=-1;
break;
}
}
if (i!=-1)
printf("Yes\n");
}
}
unsigned GetBit(unsigned num, int bit)
{
if (bit < 1) return 0;
return (unsigned)((num-((int)(num/pow((float)10,bit))) *pow((float)10,bit))/pow((float)10, bit-1)) ;
}
void main()
{
int num,len,i;
while(scanf("%d", &num))
{
len = 0;
if (!num) break;
i = num;
while( i > 0)
{
i = i/10;
len++;
}
for(i=len; i>len/2; i--)
{
if ( GetBit(num,i) != GetBit(num,len-i+1) )
{
printf("No\n");
i=-1;
break;
}
}
if (i!=-1)
printf("Yes\n");
}
}