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. js获取图片高度

    js获取图片高度时经常会获取的图片高度为0,原因是图片未加载完毕.第一次加载时,显示0(火狐等部分浏览器显示24).待加载完毕后,再刷新,显示图片高度258. var oImg = document. ...

  2. 关于C#中的DateTime类型的技巧

    * datetime.now.tostring()方法默认的你是无法得到全部的时间的格式的,只能得到日期,得不到具体时间,如果要具体时间,就应该使用 datetime的tostring()重载,dat ...

  3. 浅谈Android系统的图标设计规范

    http://homepage.yesky.com/89/11620089.shtml 目前移动平台的竞争日益激烈,友好的用户界面可以帮助提高用户体验满意度,图标Icon是用户界面中一个重要的组成部分 ...

  4. sql语句中like的使用

    先看一道题: 写出一条sql语句,找出表B中 字段Value中不全是字母 数字 下划线的数据 初看这道题,我们想到可以用like去进行模糊匹配,找出想要的结果.但是有一个地方需要注意:如果想在SQL ...

  5. Opencart 之 Registry 类详解

    Registry 中文意思是记录,登记,记录本的意思, 在opencart中他的用途就是 登记公共类.类的原型放在 system\engine文件夹下 代码很简单: <?php final cl ...

  6. oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断

    oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断 查询方式一: SELECT CASE WHEN (SELECT CAST(SUM(CASE ) THEN ...

  7. Alljoyn 概述(1)

    Alljoyn Overview Feb. 2012- AllJoyn 是什么? • 2011年2月9日发布,由 QuiC(高通创新中心)开发维护的开源软 件项目,采用 Apache license ...

  8. webViewDidFinishLoad 执行多次的问题

    在做网页加载进度条的时候,发现UIWebViewDelegate中webViewDidFinishLoad方法会执行多次: - (void)webViewDidStartLoad:(UIWebView ...

  9. [转]Delphi执行CMD命令

    今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...

  10. if....else

    if....else语句是在特定的条件下成立执行的代码,在不成立的时候执行else后面的代码. 语法: if(条件) {条件成立执行}else{条件不成立执行} 下面来写一个简单的实例 以考试成绩为例 ...