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
");
            else
                printf("No
");
        }
        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
");
            else
                printf("No
");
        }
    }
    return 0;
}

暴力出奇迹

原文地址:https://www.cnblogs.com/Draymonder/p/7325971.html