L - Prime Number(求n内质数的个数)
Description
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. 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.
Input
Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
For each dataset, prints the number of prime numbers.
Sample Input
10
3
11
Output for the Sample Input
4
2
5
解题思路:题意很简单,求n内质数的个数,注意n最大也就1e6,既可用埃氏筛也可以用欧拉筛模板,水过!
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
int n,cnt=,prime[maxn];bool isp[maxn];
void euler_sieve(){
memset(isp,true,sizeof(isp));
isp[]=isp[]=false;
for(int i=;i<maxn;++i){
if(isp[i])prime[cnt++]=i;
for(int j=;j<cnt&&i*prime[j]<maxn;++j){
isp[i*prime[j]]=false;
if(i%prime[j]==)break;
}
}
}
int main(){
euler_sieve();
while(~scanf("%d",&n)){
int num=;
for(int i=;prime[i]<=n&&i<cnt;++i)num++;
printf("%d\n",num);
}
return ;
}
L - Prime Number(求n内质数的个数)的更多相关文章
- HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)
平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...
- 求1-1e11内的素数个数(HDU 5901 Count primes )
参考链接:https://blog.csdn.net/Dylan_Frank/article/details/54428481 #include <bits/stdc++.h> #defi ...
- AOJ - 0009 Prime Number (素数筛法) && AOJ - 0005 (求最大公约数和最小公倍数)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. /* ********************* ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- 九度OJ 1040:Prime Number(质数) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- SGU 231 Prime Sum 求<=n内有多少对素数(a,b)使得a+b也为素数 规律题
题目链接:contest=0&problem=231">点击打开链接 题意: 求<=n内有多少对素数(a,b)使得a+b也为素数 思路: 我们发现全部素数间隔都是> ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
随机推荐
- centos mysql错误信息处理
mysql_secure_installation 提示错误:Enter current password for root (enter for none):ERROR 1045 (28000): ...
- [TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...
- vue-cli3取掉eslint格式提示报错
把package.json文件中关于eslint那个直接去掉. "@vue/cli-plugin-eslint": "^3.7.0",
- STM32 USB复合设备编写
目的 完成一个CDC + MSC的复合USB设备 可以方便在CDC,MSC,复合设备三者间切换 可移植性强 预备知识 cube中USB只有两个入口. main函数中的MX_USB_DEVICE_Ini ...
- ubuntu怎么打开.7z和.rar的压缩文件
通过 sudo apt-get install p7zip-rar就会自己主动安装p7zip-full和p7zip-rar两个软件. 之后就能够通过鼠标右键选择 提取到此处 直接解压压缩文件. den ...
- 深度学习笔记之CNN(卷积神经网络)基础
不多说,直接上干货! 卷积神经网络(ConvolutionalNeural Networks,简称CNN)提出于20世纪60年代,由Hubel和Wiesel在研究猫脑皮层中用于局部敏感和方向选择的神经 ...
- 我的kindle书单
刚刚入手kindle,希望能够持续阅读,不断进步. 列下书单,记录我的阅读足迹,更希望园友若有好书多多推荐,互相交流. # keep updating ... 我的kindle书单 book name ...
- Hibernate 之 HQL
通过对Hibernate框架的学习,已经慢慢的对Hibernate有了进一步的了解,接下来我们要说的是HibernateQusery Language(HQL),如果你正在学习SSH框架,那SQL对你 ...
- Email格式验证
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <html ...
- 比特币交易(Transaction)的输入与输出
比特币通过“挖矿”机制保证了不能任意造币.通过分布式网络和HashCash机制解决双重支付问题.事实上比特币系统中不存在独立的电子货币,而只存在交易单(账单),货币值是依附于交易单存在的,所以比特币中 ...