Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 167825 Accepted Submission(s): 59423
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
复制代码
1
0 1 0 0
Sample Output
复制代码
1
OK
思路:素数问题,代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include<bits/stdc++.h> #include<iostream> #include<algorithm> using namespace std; int main(){ int x,y,k,m; while(cin>>x>>y){ if(x==0&&y==0){ break; } else{ m=1; for(int i=x;i<=y;i++){ k=i*i+i+41; for(int j=2;j<k;j++){ if(k%j==0){ m=0; break; } } if(m==0){ cout<<"Sorry"<<endl; break; } } if(m==1) cout<<"OK"<<endl; } } return 0; }
最后
以上就是等待草丛最近收集整理的关于hdu 2012 素数判定的全部内容,更多相关hdu内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复