素数判定
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 159623 Accepted Submission(s): 56364
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
先暴力打个表,然后根据输入直接输出即可。
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 32 |
#include <cstdio> #include <cmath> bool a[100]; bool judge(int n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0)return false; } return true; } int main() { for (int i = -39; i <= 50; i++) a[i + 39] = judge(i*i+i+41); int x, y; scanf("%d %d", &x, &y); while ( x != y!= 0) { bool pd = true; for (int i = x; i <= y; i++) { if (a[i + 39] == false) { pd = false; break; } } if (pd) printf("OK\n"); else printf("Sorry\n"); scanf("%d %d", &x, &y); } } |