【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)
Prime Numbers
Descriptions:
A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Write a program which reads a list of N integers and prints the number of prime numbers in the list.
Input
The first line contains an integer N, the number of elements in the list.
N numbers are given in the following lines.
Output
Print the number of prime numbers in the given list.
Constraints
1 ≤ N ≤ 10000
2 ≤ an element of the list ≤ 108
Sample Input 1
5
2
3
4
5
6
Sample Output 1
3
Sample Input 2
11
7
8
9
10
11
12
13
14
15
16
17
Sample Output 2
4
题目链接:
https://vjudge.net/problem/Aizu-ALDS1_1_C
题目大意:
输入n个数,判断里面有几个是素数,逐个枚举是最简单的但是会超时,这里用了一个简单的函数,以后可以套用
bool isprime(int x)
{
if(x==)
return true;
if(x<||x%==)
return false;
int i=;
while(i<=sqrt(x))
{
if(x%i==)
return false;
i+=;
}
return true;
}
AC代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
bool isprime(int x)
{
if(x==)
return true;
if(x<||x%==)
return false;
int i=;
while(i<=sqrt(x))
{
if(x%i==)
return false;
i+=;
}
return true;
}
int n;
int main()
{
int total=;
int m;
cin>>n;
while(n--)
{
cin>>m;
if(isprime(m))
total++;
}
cout<<total<<endl;
}
这题比较简单,我之所以要为他写一篇博客,是因为一下这个埃拉托色筛选法:
1.列举大于等于2的整数
2.留下最小的整数2,删除所有2的倍数
3.在剩下的整数中留下最小的3,删除所有3的倍数
4.在剩下的整数中留下最小的5,删除所有5的倍数
5.以下同理,留下仍未被删除的最小的整数,删除该整数的倍数,一直循环到结束
int isprime[100005];
void eratos(int x)
{
for(int i=; i<=x; ++i)
isprime[i]=true;
isprime[]=isprime[]=false;
for(int i=; i<=x; ++i)
{
if(isprime[i])
{
int j=i+i;
while(j<=x)
{
isprime[j]=false;
j+=i;
}
}
}
}
这个是kuangbin的素数筛法,直接保存到数组,从1开始。
const int MAXN=;
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i=;i<=MAXN;i++)
{
if(!prime[i])prime[++prime[]]=i;
for(int j=;j<=prime[]&&prime[j]<=MAXN/i;j++)
{
prime[prime[j]*i]=;
if(i%prime[j]==)break;
}
}
}
【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)的更多相关文章
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...
- CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- UVA 10539 - Almost Prime Numbers 素数打表
Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- AOJ - 0009 Prime Number (素数筛法) && AOJ - 0005 (求最大公约数和最小公倍数)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. /* ********************* ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
随机推荐
- CUDA页锁定内存(Pinned Memory)
对CUDA架构而言,主机端的内存被分为两种,一种是可分页内存(pageable memroy)和页锁定内存(page-lock或 pinned).可分页内存是由操作系统API malloc()在主机上 ...
- HTML:描述语义
一.HTML HTML:Hypertext Markup Launguage,超文本标记语言,是网页的就文件格式,用于描述网页语义. 二.HTML骨架 DTD手册:http://www.w3schoo ...
- 防止SQL/XSS攻击
function clean($str) { $str=trim($str); $str=strip_tags($str); $str=stripslashes($str ...
- 衡量镜头解像能力性能的指标-MTF曲线
MTF(Modulation Transfer Function,模量传递函数),是目前分析镜头解像能力的方法,可以用来评判镜头还原物体对比度的能力.说到MTF,不得不先提一下衡量镜头性能的两在重要指 ...
- Qt::WindowFlags枚举类型(Qt::Widget是独立窗口和子窗口两用的,Qt::Window会有标题栏)
Qt::Widget : QWidget构造函数的默认值,如新的窗口部件没有父窗口部件,则它是一个独立的窗口,否则就是一个子窗口部件. Qt::Window : 无论是否有父窗口部件,新窗口部件都是一 ...
- Valid page threshold based garbage collection for solid state drive
A method for garbage collection in a solid state drive (SSD) includes determining whether the SSD is ...
- c语言学习笔记(7)——数组
一.为什么需要数组1.为了解决大量同类型的数据存储和使用2.为了模拟现实世界二.数组的分类1.一维数组为n个变量连续分配存储空间所有的变量数据类型必须相同所有变量所占的字节大小必须相等初始化:完全初始 ...
- 从 XML 到 XPath
XPath是 W3C(World Wide Website Consortium) 的一个标准.它最主要的目的是为了在 XML1.0 或 XML1.1 文档节点树中定位节点所设计. XPath 即为 ...
- quick-cocos2d-x游戏开发【8】——动画与动作
动画与动作,在quick中都有对其封装,所以我们还是来看一下吧. 总的来说,对于帧动画,quick封装的方法我们能够常常使用,这是很方便的,以下直接上代码来直观感受下, 比方,14张帧图片,採用coc ...
- RTB业务知识2-Open-RTB全景
一个.前言 openrtb这是一套开源的竞价广告系统,来自IAB贡献,井. 有许多值太借鉴,提供sdk api接口文档介绍,整理了相关的资料.主要包含其生态图体系.业务流程和基本的对象模型和数据模型. ...