#16287: 我自己用99990000 100000000都過了 為何??


hq8398 (一群牛)

學校 : 國立花蓮高級中學
編號 : 88824
來源 : [220.141.67.251]
最後登入時間 :
2024-03-03 10:01:37
a121. 質數又來囉 | From: [223.137.8.131] | 發表日期 : 2018-12-15 19:27

#include<stdio.h>
#include<math.h>
int main()
{
long int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
int ans=0;
for(int i=a;i<=b;i++)
{
int count=0;
for(int j=2;j<=sqrt(b);j++)
{
if(i%j==0)
count++;
}
if(count==0)
ans++;
}
printf("%d\n",ans);
}
return 0;
}

 

 
#16288: Re:我自己用99990000 100000000都過了 為何??


314159265358979323846264338327 ... (少年π)

學校 : 臺北市私立延平高級中學
編號 : 69058
來源 : [223.137.74.225]
最後登入時間 :
2024-04-18 19:26:56
a121. 質數又來囉 | From: [42.72.244.11] | 發表日期 : 2018-12-15 19:45

#include
#include
int main()
{
long int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
int ans=0;
for(int i=a;i<=b;i++)
{
int count=0;
for(int j=2;j<=sqrt(b);j++)
{
if(i%j==0)
count++;
}
if(count==0)
ans++;
}
printf("%d\n",ans);
}
return 0;
}

 

粗體是錯的地方,斜體是建議

1.sqrt(b)改成sqrt(i)
2.除了count要等於0,再加入i!=1(1不是質數)

建議:count加1次就可以break,不然會浪費時間

 

 

 
ZeroJudge Forum