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];
}
}
}
}
}

最新文章

  1. 阿里聚安全受邀参加SFDC安全大会,分享互联网业务面临问题和安全创新实践
  2. 【转载】scribe、chukwa、kafka、flume日志系统对比
  3. 在Eclipse中使用JUnit4进行单元测试(初级篇)
  4. qt-5.6.0 移植之qt源码编译
  5. 【cs229-Lecture20】策略搜索
  6. Bellman-Ford算法
  7. placehold.it-在线图片生成器(转载)
  8. linux截图工具scrot
  9. 深入浅出 Java Concurrency (4): 原子操作 part 3 指令重排序与happens-before法则
  10. -_-#【AJAX】XMLHttpRequest
  11. python-整理--使用IDE
  12. scala akka 修炼之路5(scala特质应用场景分析)
  13. Linux的网卡由eth0变成了eth1,如何修复
  14. (转)Java并发编程:并发容器之ConcurrentHashMap
  15. 第二篇:数据可视化 - 基本API
  16. box-sizing属性(指定针对元素的宽度与高度的计算方法)
  17. Spring事物管理--相关要点及配置事物管理器
  18. CSS兼容性(IE和Firefox)技巧
  19. 《Python黑帽子:黑客与渗透测试编程之道》 Scapy:网络的掌控者
  20. beat冲刺(4/7)

热门文章

  1. myEclipse修改deploy location
  2. 利用SQLiteOpenHelper创建数据库,进行增删改查操作
  3. Member var and Static var.
  4. dnw for linux: Ubuntu下可用,无需编译驱动,mini2440可用
  5. Deep Learning 学习随记(七)Convolution and Pooling --卷积和池化
  6. 【POJ2185】【KMP + HASH】Milking Grid
  7. javascript——迭代方法
  8. MYSQL根据分类分组取每组一条数据且按条件能排序的写法
  9. Linux的压缩解压命令快速上手——解压篇
  10. php 购物车完整实现代码