hdu_2012_素数判定

来源:互联网 发布:淘宝商品删除怎么恢复 编辑:程序博客网 时间:2024/06/11 20:09

2012真是个不祥的数字啊...一开始语句顺序就错了,后来浪费了好多时间....这次我水了

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): 30443    Accepted Submission(s): 10397


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
 
        1. #include<iostream>
        2. #include <cmath>
        3. using namespace std;
        4. int main()
        5. {
        6. int x,y,temp,flag,i,j,t;
        7. double k;
        8. while(cin>>x>>y&&x||y)
        9. {
        10. flag=0;
        11. if(x>y){temp=x;x=y;y=temp;}
        12. for(t=x;t<=y;t++)
        13. {
        14. i=t*t+t+41;
        15. k=sqrt((double)i);
        16. for(j=2;j<=k;j++)
        17. {
        18. if(i%j==0)
        19. {
        20. flag++;
        21. break;
        22. }
        23. }
        24. }
        25. if(flag==0)
        26. cout<<"OK"<<endl;
        27. else
        28. cout<<"Sorry"<<endl;
        29. }
        30. return 0;
        31. }