6.素数和(素数的判断)

题目描述 Description

给定2个整数a,b 求出它们之间(不含a,b)所有质数的和。

输入描述 Input Description

一行,a b(0<=a,b<=65536)

输出描述 Output Description

一行,a,b之间(不含a,b)所有素数的和。

样例输入 Sample Input

39 1224

样例输出 Sample Output

111390

数据范围及提示 Data Size & Hint

注意没有要求a

源代码如下:

#include

#include

using namespace std;

#include

long long sum=0;

int main()

{

int a,b;

cin>>a>>b;

if(a>b)

swap(a,b);//考虑a>b的情况

for(int i=a;i<=b;++i)

{

int flag=0;//标志位

if(i==1)//考虑1不是素数的情况,因为a,b都可以从0开始

continue;

for(int j=2;j<=sqrt(i);++j)

{

if(i%j==0)

{

flag++;

break;

}

}

if(flag==0)

sum+=i;

}

cout<<sum<<endl;

return 0;

}

原文地址:https://www.cnblogs.com/csgc0131123/p/5290585.html