您的位置:首页 > 其它

hdu-2138- How many prime numbers

2013-11-28 23:23 375 查看
Problem Description

  Give you a lot of positive integers, just to find out how many prime numbers there are.

 

Input

  There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.

 

Output

  For each case, print the number of prime numbers you have found out.

 

Sample Input

3
2 3 4

 

Sample Output

2

 

意思就是说给了多组数字,每组都有很多数字,让你判断每组里的素数有多少个,方法很简单,不过不管是用c还是java,貌似都是用sqrt函数比较快些,大概就是这样,具体代码如下

import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin=new Scanner(new BufferedInputStream(System.in));
int num,sum,n;
boolean isprime;
while(cin.hasNext())
{
num=cin.nextInt();
sum=0;
for(int i=1;i<=num;++i)
{
n=cin.nextInt();
if(n==1)    continue;
if(n<4)
{
++sum;
continue;
}
isprime=true;
double tem=Math.sqrt(n);
for(int j=2;j<=tem;++j)
{
if(n%j==0)
{
isprime=false;
break;
}
}
if(isprime)    ++sum;
}
System.out.println(sum);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: