hdu acm-step 2.1.2 How many prime numbers

 

   本题题意:给出n个数,求其中素数的个数.

   代码如下:

   

#include <cstdio>
#include <cmath>
using namespace std;
bool prime(int n)
{
        if(n == 2)return true;
        if(n % 2==0)return false;
        int s = sqrt(n);
        int i;
        for(i=3;i<=s;i+=2)
        {
                if(n%i==0)break;
        }
        if(i > s)return true;
        return false;
}
int main()
{
        int n;
        while(scanf("%d",&n)==1)
        {
                int a,count=0;
                for(int i=0;i<n;i++){scanf("%d",&a);if(prime(a))count++;}
                printf("%d\n",count);
        }
        return 0;
}

由于本题的上限可达31bit,所以打表是不适用了,只能写个素数判定排除偶数,然后开方,直接暴力目测会超时。

posted @ 2017-08-19 20:11  mtl6906  阅读(145)  评论(0编辑  收藏  举报