杭電oj2012 c++
阿新 • • 發佈:2019-01-25
題目簡介:
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 Input0 1 0 0
Sample OutputOK
水題,但我的程式碼還是寫的不好,先這樣吧。以後漲知識了會再回來改的
AC程式碼如下:
#include<iostream> #include<math.h> using namespace std; int judge1(int a){ for(int i=2;i<sqrt(a);i++){ if(a%i==0) return 0; } return 1; } int judge2(int a,int b){ int t; for(int i=a;i<=b;i++){ t=i*i+i+41; if(judge1(t)==0){ return 0; } } return 1; } int main() { int a,b,t; while(cin>>a>>b){ if(a==0&&b==0) break; if(judge2(a,b)==0) cout<<"Sorry"<<endl; else cout<<"OK"<<endl; } }