2019 北航上机习题一
给定区间[a,b],要求输出素数的等差序列,三个以上才算是序列,例如[100,200],会输出 151 157 163 .
#include<stdio.h>
#include<math.h>
int function(int n)
{
if(n<2)
{
return -1;
}
if(n==2){
return 0;
}
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
return 1;
}
}
return 0;
}
int next(int n)//素数n的下一个素数
{
int result=n+1;
while(function(result)==1)
{
result++;
}
return result;
}
int main()
{
int a,b;
scanf("%d",&a);//左区间
scanf("%d",&b);//右区间
int first=0;
int second=0;
int dif=0;
//printf("%d\n",function(4));
for(int i=a;i<b;i++)
{
if(function(i)==0)
{
first=i;
if(next(i)<b)
{
second=next(i);
}
else{break;}
dif=second-first;
if(next(second)-second==dif&&next(second)<b)
{
printf("%d %d %d ",first,second,next(second));
second=next(second);
while(next(second)-second==dif&&next(second)<b)
{
printf("%d ",next(second));
second=next(second);
}
printf("\n");
}
}
}
//printf("%d %d %d ",function(167),function(173),function(179));
return 0;
}
OPPO公司福利 1202人发布