shandy @ 2023-10-06 03:22:29
c语言求大佬帮看看,最后三个tle
#include <stdio.h>
int any(int a)
{
int k;
if (a == 1)
{
return 0;
}
if (a % 2 == 0)
{
return 0;
}
for (int j = 2; j < a - 1 && j <= 9989899; j++)
{
if (a % j == 0)
{
k = 0;
break;
}
else
{
k = 1;
}
}
if (k == 1)
{
printf("%d\n", a);
}
}
int some(int a)
{
int c = a, b = 0;
while (c)
{
b *= 10;
b += c % 10;
c = c / 10;
}
return b == a;
}
int i, n, start;
int main()
{
scanf("%d %d", &start, &n);
for (i = start; i <= n; i++)
{
if (some(i))
{
any(i);
}
}
return 0;
}
by yzl0225 @ 2023-10-06 08:01:43
要加一个位数判断,有偶数位的回文数(除了11)必然不是质数,因为他们都是11的倍数了,可以先判断是否是偶数位且不是11
by danlao @ 2023-10-06 08:04:52
@shandy 你一个一个枚举当然超时了,你应该先用循环来构造回文数,再来看它是不是质数了。(题目最后一段有提示)
by yzl0225 @ 2023-10-06 08:06:13
判断的代码(记得在主函数中加上判断):
int ws(int x)
{
if(x>=10 && x<100 && x!=11 || x>=1000 && x<10000) return 0;
if(x>=100000 && x<1000000 || x>=10000000 && x<100000000) return 0;
return 1;
}
by yzl0225 @ 2023-10-06 08:07:40
@yaodiguoan 我这样也没错吧(我只是路过的)?
by yzl0225 @ 2023-10-06 08:10:29
@shandy,2023-10-06 03:22发的?那时候应该没人会回的啊
by danlao @ 2023-10-06 08:27:29
可是你只判断了11,另外还有很多种情况,你一个一个枚举,不超时才怪。
1亿中11的倍数的个数有:floor(100000000/11)=9090909个,另外还有90909091个数不是11的倍数,你只判断了11的倍数,不超时才怪
by shandy @ 2023-10-06 11:33:11
@yaodiguoan ok谢谢
by shandy @ 2023-10-06 11:35:14
@yaodiguoan 一语点醒 给你大拇哥
by shandy @ 2023-10-06 11:35:40
@yzl0225 欧克