当前位置:网站首页>PAT乙级-B1013 数素数(20)
PAT乙级-B1013 数素数(20)
2022-08-03 14:20:00 【nekoha_dexter】
令 Pi 表示第 i 个素数。现任给两个正整数 M≤N≤104,请输出 PM 到 PN 的所有素数。
输入格式:
输入在一行中给出 M 和 N,其间以空格分隔。
输出格式:
输出从 PM 到 PN 的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。
输入样例:
5 27
输出样例:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1000000
bool PrimeTable[maxn];
//1、埃氏筛法,在过程中对素数进行编号
//2、当第M个素数,开始考虑输出
//2.1、输出时,考虑没十个或第N个都要输出回车
//2.2、输出后考虑是否已到第N个了,是则跳出
//3、maxn 建议到10^6,否则会出现段错误,无法ac
void get(const int m, const int n){
int cnt = 0;
for(int i = 2; i <= maxn; ++i)
if(PrimeTable[i]){
cnt++;
if(cnt >= m){
cout << i;
if((cnt - m + 1) % 10 == 0 || cnt == n) cout << endl;
else cout << " ";
}
if(cnt >= n) return;
for(int j = i * 2; j <= maxn; j +=i)
PrimeTable[j] = false;
}
}
int main(){
int m, n;
cin >> m >> n;
fill(PrimeTable, PrimeTable + maxn, true);
get(m, n);
}
边栏推荐
- 有哪些好用的IT资产管理平台?
- [web penetration] detailed explanation of CSRF vulnerability
- 致一位湖南女孩
- VMware 虚拟机如何连接网络「建议收藏」
- varchar2和varchar2(char)_datetime数据类型
- Leetcode 448. Find All Numbers Disappeared in an Array to Find All Disappeared in an Array of Numbers (simple)
- CVPR 2022 | Predicting Skeletons from Human Meshes, True Physiological Skeletons!
- 利用华为云ECS服务器搭建安防视频监控平台【华为云至简致远】
- STL——vector
- 【报错】ImportError: libpng16-57e5e0a0.so.16.37.0: cannot open shared object file: No such file or direc
猜你喜欢
随机推荐
MySQL知识总结 (十二) 数据库相关概念
数据分析(二)——numpy
可视化数据库设计软件有哪些_数据库可视化编程
“芯片法案”通过后,美光承诺在美国扩产
为什么手动启动GBase 8c数据库中GTM节点,起不来。显示“Run cmd failed:scp: /tmp/gtm_gtm1.server: Permission denied”
PCL 点云按时间进行分段
英文邮件总结
Forrester:行业云帮助中国企业更快适应未来的发展
LeetCode169:多数元素
How to connect a VMware virtual machine to the network "recommended collection"
15年软件架构师经验总结:在ML领域,初学者踩过的5个坑
Huffman tree
ideaIU-2020.1下载
【web渗透】CSRF漏洞详细讲解
想成为网络安全技术爱好者(可能是黑客)的话,需要看什么书?
1, introduction to petri net
回流和重绘
varchar2 and varchar2(char)_datetime data types
【报错】ImportError: libpng16-57e5e0a0.so.16.37.0: cannot open shared object file: No such file or direc
【框架】idea找不到xxx依赖项怎么办