40026118素数的个数
2015-11-18 17:16
417 查看
40026118素数的个数 |
试题描述 |
给定整数 n ,请你统计不超 n 的素数的个数。 |
输入 |
仅包含一个正整数 n 。 |
输出 |
不超过 n 的素数的个数。 |
输入示例 |
11 |
输出示例 |
5 |
其他说明 |
数据范围:1 <= n <= 10^6 。 样例说明:有 2、3、5、7、11 共 5 个素数。 |
#include<iostream> #define LL long long #define maxn 1000010 using namespace std; LL read() { LL x=0,f=1; char c=getchar(); while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } bool prime[maxn]; LL a; void prime_table() { for(int i=2;(LL)i<=a;i++) prime[i]=1; for(int i=2;(LL)i*i<=a;i++) if(prime[i]) for(LL j=i*i;j<=a;j+=i) prime[j]=0; return; } int main() { a=read(); prime_table(); int cnt=0; for(int i=2;i<=a;i++)if(prime[i]) cnt++; printf("%d\n", cnt); system("pause"); return 0; }
View Code
相关文章推荐
- ZBX_NOTSUPPORTED: Item does not allow parameters.
- php 数组 (3) reset() end() count() current() key()
- Redirect url 路径简单介绍
- iOS 键盘变中文
- 骨牌铺方格
- 05 EIGRP
- php使用exec函数导入导出数据库
- 排序数组构造AVL树
- aliyun sources.list of ubuntu 14.04
- log4j日志记录到数据库
- do_softirq
- Windows下使用Beyond Compare作为git的比对与合并工具
- UE4 Console Commands List
- 关于GD32F107的eth、mac、phy的问题
- 已有 JS 模块化和打包方案收集
- php-fpm的安装和启动
- java判断质数
- 老李分享:大数据测试中java和hadoop关系
- 如果想要获得最近1小时访问服务器的请求数?
- 利用forever确保nodejs在后台运行,不会down掉