代码:
#include <stdio.h>
#include<math.h>
#include<stdbool.h>
bool Palindrome(int x)//判断一个数是否为回文数
{
int t = 0;
int a = x;//存放原数据
while(x !=0)
{
t = t*10 + x%10;
x /= 10
}
if(x == a)
{
return true;
}
else return false;
}
bool Prime(int x)//判断一个数是否为素数
{
for(int i = 2;i < x;i++)
{
if(x%i == 0)
return true;
}
}
int main()
{
bool x,y;
int a,b;
scanf("%d %d",&a,&b);
for(int i = a;i<=b;i++)
{
x = Palindrome(i);
y = Prime(i);
if(x == y == true)
{
printf("%d\n",i);
}
}
return 0;
}
关于回文算法的设计
bool Palindrome(int x)
{
int t = 0;
int a = x;//存放原数据
while(x !=0)
{
t = t*10 + x%10;
x /= 10;
}
if(x == a)
{
return true;
}
else return false;
}
思路:
- 设计一个bool返回值类型的函数,调用时导入一个整形数据;
t = 0时,假设x = 1221;第一次循环时,t = 1,x = 122;第二次循环时,t = 12,x = 12;第三次循环时t = 122,x = 1;第四次循环时t = 1221,x = 0;
但这个算法速度不够快,做题的时候显示output limit exceeded。