素数判定
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 117640 Accepted Submission(s): 41592
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 1
0 0
Sample Output
OK
Author lcy
Source C语言程序设计练习(二)
Recommend JGShining | We have carefully selected several similar problems for you: 2098 1262 1000 1431 1001
#include<stdio.h>
#include<math.h>
int main() {
int a,b,c,i,j;
while(scanf("%d %d",&a,&b),a!=0||b!=0) {
int d=0;
for(i=a; i<=b; i++) {
c=i*i+i+41;
for(j=2; j<=(int)sqrt(c); j++) {
if(c%j==0)
break;
}
if(j<=(int)sqrt(c)) {
printf("Sorry\n");
break;
}
if(i==b&&j>(int)sqrt(c))
d=1;
}
if(d==1)
printf("OK\n");
}
return 0;
}
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2012