1106 】質數檢測 (水題,數論)
阿新 • • 發佈:2018-12-21
題幹:
給出N個正整數,檢測每個數是否為質數。如果是,輸出"Yes",否則輸出"No"。
Input
第1行:一個數N,表示正整數的數量。(1 <= N <= 1000) 第2 - N + 1行:每行1個數(2 <= Sii <= 10^9)
Output
輸出共N行,每行為 Yes 或 No。
Sample Input
5 2 3 4 5 6
Sample Output
Yes Yes No Yes No
解題報告:
不能預處理,會超時的。每次sqrt(n)就足夠了。sqrt(1e9)=30000左右,這樣總最差複雜度就是o(1000*30000)也就是個1e7。不會炸,但是如果n*log(logn)的預處理就不太辦了。。
AC程式碼:
#include<bits/stdc++.h> using namespace std; bool isprime(int x) { for(int i = 2; i*i<=x; i++){ if(x%i==0) return 0; } return 1; } int main() { int n; int qq; cin>>n; while(n--) { scanf("%d",&qq); if(isprime(qq) ) printf("Yes\n"); else printf("No\n"); } return 0 ; }