素数判定
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu 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
//主要是想说一下sqrt(x)精度损失问题。 //floor向下取整(包含在assert.h头文件中)。 1 #include2 #include 3 #include 4 #include 5 6 int judge(int x)//判断一个数是否为素数//logn 7 { 8 if(x==0 || x==1) return 0; 9 int m = floor(sqrt(x)+0.5);10 for(int i=2; i<=m; i++)11 {12 if(x%i==0)13 {14 return 0;15 }16 }17 return 1;18 }19 int main()20 {21 int x, y, i, k, cnt;22 int s[10000];23 while(~scanf("%d%d", &x, &y) )24 {25 if(x==0 && y==0) break;26 k = 0;27 cnt = 0;28 memset(s, 0, sizeof(s));29 for(i=x; i<=y; i++)30 {31 s[k++] = i*i+i+41;32 if(s[k-1]<0)33 s[k-1] = -s[k-1];34 if(judge(s[k-1]))35 cnt++;36 }37 if(cnt == y-x+1)38 printf("OK\n");39 else printf("Sorry\n");40 }41 return 0;42 }