首页 > 代码库 > 51nod 1106 质数检测

51nod 1106 质数检测

#include <bits/stdc++.h>using namespace std;int n;const int maxn = 1e5+10;bool s[maxn];void is_prime(){    memset(s,true,sizeof(s));    s[0] = s[1] = 0;    for(int i=2; i*i <= maxn;i++){        if(s[i]){            for(int j=i*i; j <= maxn;j += i)                s[j] = 0;        }    }}int main (){    scanf("%d" ,&n);    is_prime();    int x;    for(int i=1; i <= n;i++){        scanf("%d", &x);        if( x <= maxn){            if( s[x] )                printf("Yes\n");            else                printf("No\n");        }        else{            bool ok = 1;            for(int i=2;i*i <= maxn;i++){                if(s[i] && x%i==0){                    ok = 0;                    break;                }            }            if(ok)                printf("Yes\n");            else                printf("No\n");        }    }    return 0;}

暴力出奇迹

51nod 1106 质数检测