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

这个题目就是让你求一组的素数有多少个。

这个素数范围的数字有点大,所以不能用打表。

测试数据很水。。。直接判断就能过了。

不过判断的时候,有一个地方需要注意的,我在那个判断素数的方法注释了。

import java.util.Arrays;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
//boolean db[] = new boolean[2147483647];
//数组太大,不能打表!
//dabiao(db);
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
long sum = 0;
int m;
for(int i=0;i<n;i++){
m=sc.nextInt();
if(prime(m)){
sum++;
}
}
System.out.println(sum);
}
} //直接判断能过,说明数据比较水。
private static boolean prime(int m) {
for(int i=2;i<=Math.sqrt(m);i++){
//***** 注意:i*i<=m 是会超时的,因为i*i每次都要计算
if(m%i==0){
return false;
}
}
return true;
} //素数筛选打表应该会超时
private static void dabiao(boolean[] db) {
Arrays.fill(db, true);
for(int i=2;i<=Math.sqrt(db.length);i++){
for(int j=i+i;j<db.length;j+=i){
if(db[j]){
db[j]=!db[j];
}
}
}
}
}

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)的更多相关文章

  1. HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

    How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU 2138 How many prime numbers (判素数,米勒拉宾算法)

    题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...

  3. HDU 2138 How many prime numbers

    米勒罗宾素数测试: /* if n < 1,373,653, it is enough to test a = 2 and 3. if n < 9,080,191, it is enoug ...

  4. HDOJ/HDU 2710 Max Factor(素数快速筛选~)

    Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...

  5. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  6. CodeForces 385C Bear and Prime Numbers 素数打表

    第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...

  7. UVA 10539 - Almost Prime Numbers 素数打表

    Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In ...

  8. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  9. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

随机推荐

  1. CSS样式权值

    内联样式表(InLine style)>内部样式表(Internal style sheet)>外部样式表(External style sheet) 例外:但如果外部样式表放在内部样式表 ...

  2. ORACLE每组只保留一条记录

    删除同一组内其他记录 DELETE from memactivities a where exists(select 1 FROM (select Uuid,ci_no,lst_upd_ts,ROW_ ...

  3. Android 4.0及以上版本接收开机广播BOOT_COMPLETED、开机自启动服务

    1.BootCompletedReceiver.Java文件 public class BootCompletedReceiver extends BroadcastReceiver { @Overr ...

  4. asp.net中ashx文件如何调用session

    如果你要保证数据的安全性,你可以在ashx中使用session验证.如:你的index.aspx中使用jquery回调ashx数据,那么在index.aspx page_load时session[&q ...

  5. 如何设置MySQL Workbench EER Diagram 尺寸?

    ER Diagram -> Model -> Diagram Properties and Size...

  6. IE浏览器设置

  7. 非注解SpringMVC

    <!-- SpringMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> < ...

  8. ubuntu 升级命令

    apt-get update && apt-get dist-upgrade

  9. JQuery对单选框,复选框,下拉菜单的操作

    JSP <%@ page language="java" import="java.util.*" pageEncoding="utf-8&qu ...

  10. php把文件上传到远程服务器上例子

    在这里我们利用curl实现把本地服务器的文件通过curl发送请求给远程服务器的php文件接受就实现了上传,还一个是利用ftp来上传方法也是php中的curl操作ftp服务器进行上传. 我这里写的是用c ...