杭电ACM2012:素数判定

来源:互联网 发布:linux 安装gcc4.8 编辑:程序博客网 时间:2024/06/05 10:13
#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;}
0 0