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

最新文章

  1. ScrollView中嵌套GridView,ListView只显示一行的解决办法
  2. 如何读懂复杂的C语言声明
  3. BZOJ 2763 分层图最短路
  4. 低功耗蓝牙4.0BLE编程-nrf51822开发(3)
  5. mybatis缓存创建过程
  6. [React Fundamentals] Introduction to Properties
  7. java中怎么进行字符串替换?
  8. c语言字符串翻转系列
  9. Java使用freemarker导出word和excel
  10. 朝花夕拾-4-shell
  11. java的List列表转成Tree(树形)结构列表
  12. 《深度探索C++对象模型》读书笔记(一)
  13. 一.javascript核心部分:1.词法结构
  14. 在docker中部署centos7镜像
  15. JAVA连接数据库 #03# HikariCP
  16. nginx反向代理配置相对路径
  17. echart生成饼状图
  18. Spring框架简介
  19. Oracle数据库的语句级读一致性
  20. 谷歌发布&quot;自动机器学习&quot;技术 AI可自我创造

热门文章

  1. C++设计模式之适配器模式(二)
  2. python05-09
  3. python第四讲
  4. MySQL基础笔记(三) 复杂查询
  5. 块状元素的text-align对齐属性
  6. 使用Blender批量导出/转换模型
  7. windows下的两个等待函数
  8. Sql数据库查询语言
  9. How can I pass data from Flask to JavaScript in a template?
  10. 定时邮件 已经稳定运行10天+ 从局域网linux到外网邮箱