您的位置:首页 > 其它

杭电ACM2012:素数判定

2017-04-12 21:04 295 查看
#include <iostream>
#include <cmath>
using namespace std;
int isPrime(const int a){
int flag = 1;
int b = a;
for (int i = 2; i < sqrt(b); i++){
if (b%i == 0) flag=0;
}
return flag;
}
int main(){
int n,x,y,s=0;
while ((cin >> x >> y) && (x != 0 || y != 0))
{
if (x > y){
x = x + y;
y = x - y;
x = x - y;
}
for (int n = x; n <= y; n++){
int m = n*n + n + 41;
if (isPrime(m)) s++;
else break;
}
if (s == y - x + 1) cout << "OK" << endl;
else cout << "Sorry" << endl;
s = 0;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: