#include
int main()
{
int a,b,i,t,c=0,d;
scanf("%d %d",&a,&b);
if (a<b)
{
t=a;
a=b;
b=t;
}
for (i=b;i<a+1;i++)
{
for(d=0;d<i+1;d++)
{
if(i%d==0)
{
c++;
}
}
if (c==2)
{
printf("%d",i) ;
}
}
return 0;
}
我想找出 素数 为啥程序运行不起来
运行不起来贴个报错的图也方便大家啊
这不出来了个2和9 吗?怎么就运行不起来了啊
include<stdio.h>
include<math.h>
int a,b;
void swap(){
int t=a;
a=b;
b=t;
}
int main(){
int i,j;
scanf("%d%d",&a,&b);
if(a>b) swap();
for(i=a;i<=b;i++){
int f=1;
if(i==0||i==1) continue;
for(j=2;j<=sqrt(i);j++)
if(i%j==0){
f=0;
break;
}
if(f) printf("%d ",i);
}
return 0;
}
C++在主程序中可以直接用swap(a,b)和bool。
推荐j从2开始,到sqrt(i)
2是第一个质数,i最多能分成sqrt(i)*sqrt(i),因为i除以一个大于sqrt(i)的数,商一定小于sqrt(i),在前面一定已经检查到了。
sqrt(i)是根号i,要加math.h
一旦遇到一个j能整除i,就直接跳出。
逻辑上的问题 你的逻辑实在有点。。