易错:
1、题目没有提a,b大小关系,若不判断大小,会导致wrong answer;
2、素数判断flag中要判断是否为1;
Description:
人与龙和谐相处的博克岛上正在举办喜羊羊版的魁地奇,龙骑士们通过抓羊和把羊投入篮筐来得分,如果能抓到皮肤黝黑的沸羊羊还能获得额外的分数奖励。
当然,如果比分这样算就太没意思了,现在规定一种新的得分机制:假设篮筐中黑羊为a只,白羊为b只,那他们的得分就是a,b之间(不含a,b)所有素数的和。由于数字可能比较大,现邀你来完成最后得分的计算。
Input:
有多组测试样例,输入直到文件末尾。
每组样例占一行, a b (0<=a,b<=65536)
Output:
对每组样例输出占一行,输出内容为a,b之间(不含a,b)所有素数的和。
Sample Input:
2 10
23 90
39 1224
Sample Output:
15
863
111390
#include <iostream>
#include "cmath"
using namespace std;
int main() {
int black, white;
while (cin >> black >> white) {
if (black > white) {
swap(black, white);
}
int count = 0;
for (int i = black + 1; i < white; ++i) {
bool flag = true;
for (int j = 2; j <= sqrt(i); ++j) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag && i > 1) {
count += i;
}
}
cout << count << endl;
}
return 0;
}