现在的位置: 首页 > 综合 > 正文

HDU 2138

2012年06月09日 ⁄ 综合 ⁄ 共 353字 ⁄ 字号 评论关闭

刷水题

判断有多少个素数。关键点避免超时。

http://acm.hdu.edu.cn/showproblem.php?pid=2138
View Code

#include <stdio.h>
#include <math.h>

int isPrimer(unsigned int n )
{
int i ;
unsigned int m = sqrt(n);
for(i = 2; i <= m; ++i)
{
if(!(n % i ))
break;
}
return i > m;
}
int main()
{
int n,m;
int count,i;

while(scanf("%d",&n)==1)
{
count=0;
for(i=0;i<n;i++)
{
scanf("%d",&m);
if(isPrimer(m))
count++;
}

printf("%d\n",count);
}

return 0;
}

抱歉!评论已关闭.