这道题~以前判定prime是一个个去试着整除再去存储,上次弄过欧拉函数那题目之后就知道了,这样会更快捷:
1 prime[0] = prime[1] = 1; 2 for(int i = 2; i <maxn; i++) 3 { 4 if(!prime[i]) 5 { 6 for(int j = i * 2; j < maxn; j += i) 7 prime[j] = 1; 8 } 9 }
以下是AC代码~~~水水题~
1 #include<iostream> 2 #include<memory.h> 3 using namespace std; 4 #define maxn 3000 5 int prime[maxn]; 6 void f() 7 { 8 memset(prime,0,sizeof(prime)); 9 prime[0] = prime[1] = 1; 10 for(int i = 2; i <maxn; i++) 11 { 12 if(!prime[i]) 13 { 14 for(int j = i * 2; j < maxn; j += i) 15 prime[j] = 1; 16 } 17 } 18 } 19 int main() 20 { 21 int n,m; 22 int f; 23 while(cin>>n>>m && (n + m)) 24 { 25 f = 0; 26 for(int i = n; i <= m; i++) 27 if(prime[i*i+i+41]) 28 { 29 f = 1; 30 break; 31 } 32 if(f) 33 cout << "Sorry" << endl; 34 else 35 cout << "OK" << endl; 36 } 37 return 0; 38 }
时间: 2024-10-13 06:43:28