所谓回文数就是将一个数从左向右读与从右向左读是一样的,例如,121和1331都是回文数。编写一个函数实现求正整数n以内的回文数。
可参https://mp.csdn.net/console/editor/html/27976119
这个只需要将整数转换成字符串,然后左右两端逐个比较就可以了
#include <string.h>
#include <stdlib.h>
void main()
{
int n;
scanf("%d",&n);
printf("%d以内的回文有:\n",n);
for(int j=0;j<=n;j++)
{
char str[20];
itoa(j,str,10);
bool b = true;
int len = strlen(str);
for(int i=0;i<len/2;i++)
{
if(str[i] != str[len-i-1])
{
b = false;
break;
}
}
if(b)
printf("%d ",j);
}
}