How many prime numbers(題目鏈接)
思路
開(kāi)始使用篩法赏僧,但是沒(méi)有AC
代碼如下
#include <iostream>
#include <math.h>
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;
}