hdu 2012 素数判定

来源:互联网 发布:索尼电视软件 编辑:程序博客网 时间:2024/04/30 13:16

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2012

素数判定

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 83904    Accepted Submission(s): 29194


Problem Description
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。
 

Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
 

Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。
 

Sample Input
0 10 0
 

Sample Output
OK
 

Author
lcy
 

Source
C语言程序设计练习(二)
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  2024 1262 1096 1089 1431 
 

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;bool IsPrime(int a){    int d = (int)sqrt(a);    for(int i = 2; i <= d; ++i)        if(a % i == 0)            return false;    return true;}int main(){    int x, y, iCur;    bool flag;    while(~scanf("%d %d", &x, &y))    {        if(x == 0 && y == 0)            break;        flag = true;        for(int i = x; i <= y; ++i)        {            iCur = i * i + i + 41;            if(!IsPrime(iCur))            {                flag = false;                break;            }        }        if(!flag)            printf("Sorry\n");        else            printf("OK\n");    }    return 0;}



0 0
原创粉丝点击