How many prime numbers

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14684    Accepted Submission(s): 5091

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
 

题解:

水暴力,打表不行,由于数据量太大,运行到sqrt(x)就可以;

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
bool js(int x){
if(x==)return true;
if(x==||x==)return false;
if(x%==)return false;
for(int i=;i<=sqrt(x);i+=){
if(x%i==)return false;
}
return true;
}
int main(){
int N,a;
while(~scanf("%d",&N)){
int cnt=;
for(int i=;i<N;i++){
scanf("%d",&a);
if(js(a))cnt++;
}
printf("%d\n",cnt);
}
return ;
}

How many prime numbers(求素数个数)的更多相关文章

  1. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

  2. Sum of Consecutive Prime Numbers(素数打表+尺取)

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

  3. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

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

  4. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  5. 【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)

    Prime Numbers  Descriptions: A prime number is a natural number which has exactly two distinct natur ...

  6. LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)

    题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...

  7. How many prime numbers(素数)

    Problem Description   Give you a lot of positive integers, just to find out how many prime numbers t ...

  8. leetcode 204题求素数个数

        Description: Count the number of prime numbers less than a non-negative number, n 提示晒数法: http:// ...

  9. UVA1210Sum of Consecutive Prime Numbers(素数打表 + 连续和)

    题目链接 题意:输入一个数n (2 <= n <= 10000) 有多少种方案可以把n写成若干个连续素数之和 打出10000之内的素数表,然后再打出每个可能得到的和的方案数的表 #incl ...

随机推荐

  1. rownum(转载)

    对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=),并非说用>,>=,=,be ...

  2. c++实现单例

    单例宏: //单件定义宏 //------------------------------------- // 在头文件中申明 // DECLARE_SINGLEOBJ( CSampleClass ) ...

  3. csapp lab3 bufbomb 缓存区溢出攻击 《深入理解计算机系统》

    这个实验主要是熟悉栈,和了解数据缓存区溢出的问题. 数据缓存区溢出:程序每次调用函数时,会把当前的eip指针保存在栈里面,作为被调用函数返回时的程序指针.在被调用程序里面,栈是向下增长的.所有局部变量 ...

  4. Labview中引用,属性节点,局部变量之间的区别

    Labview中引用,属性节点,局部变量之间的区别        在Labview中我们经常会碰到这样几个概念,那就是控件的引用,属性节点以及局部变量,他们之间到底有哪些区别呢?        首先谈 ...

  5. day6_python学习笔记_chapter8_条件,循环

    1. if elif else 2. 条件表达式:三元操作符: smaller = x if x < y else y   == if x < y : smaller =x  else : ...

  6. LintCode-数组划分

    题目描述: 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i,满足 ...

  7. Dos命令之Netsh

    NetSH (Network Shell) 是windows系统本身提供的功能强大的网络配置命令行工具. 常用命令 1. 导出配置脚本:netsh -c interface ip dump > ...

  8. linux系统性能监控常用命令

      一.Linux服务器性能关注点 1)CPU -> load:表示cpu在一段时间内正在处理以及等待处理的任务之和统计信息,简单可理解为cpu正处理的线程数和能同时处理的线程数的比值.一般认为 ...

  9. Android 优化性能之 如何避免--过度绘制

    可能有些人不明白什么是过度绘制,简单言,我们app一个页面所显示的效果是由像素一帧一帧绘制而成.过度绘制就是意味着这一帧被绘制多次.如果是静态的布局,可能影响不是很大,如果是动态的,比如ListVie ...

  10. U盘重装系统win7

    步骤: 1开机f12进入bios模式设置光盘的第一驱动 2U盘拷贝好的ghost镜像插入usb接口,重启默认U盘启动 3启动后进入重装系统界面,选择要重新的系统,进入pe系统,格式化c盘,后面默认自动