预计阅读时间:4分钟
原题
描述
输入两个整数X和Y,输出两者之间的素数个数(包括X和Y)。
输入
两个整数X和Y(1 <= X,Y <= 105)。
输出
输出一个整数,表示X,Y之间的素数个数(包括X和Y)。
样例输入
1 100
样例输出
25
题目分析
题目很好懂,无须分析。
思路
判断素数可以从2到sqrt(i),这样速度最快。
参考代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char a[10001];
int b[10001];
int maxn=-1;
char ans=0;
int beginn;
int endn;
int now;
int tot;
int flag=0;
int main()
{
int x,y;
cin>>x>>y;
for(int i=min(x,y); i<=max(x,y); i++)
{
flag=0;
for(int j=2; j<=sqrt(i); j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
tot++;
}
}
if(min(x,y)==1)
{
cout<<tot-1;
}
else
{
cout<<tot;
}
return 0;
}
希望本博客对大家有所帮助!