【HDU 2138】How many prime numbers

How many prime numbers(题目链接)

思路

开始使用筛法,但是没有AC

代码如下

#include 
#include 
using namespace std;
#define LOCAL 0
/*
 * 使用筛法RE
*/
int isPrime(int x){
    int tmp = (int)sqrt(x * 1.0);
    for(int i = 2;i <= tmp;i++){
        if(x%i == 0){
            return 0;
        }
    }
    return 1;
}

int main(){   
#if LOCAL
    freopen ("datain.txt","r",stdin);
    freopen ("dataout.txt","w",stdout);
#endif
    
    int n;
    while(cin>>n){
        int count = 0; 
        int tmp;
        for(int i = 0;i < n;i++){
            scanf("%d",&tmp);
            if(isPrime(tmp)){
                count++;
            }
        }
        cout << count << endl;
    }
    
    return 0;
}

你可能感兴趣的:(【HDU 2138】How many prime numbers)