博客
关于我
B-1013 数素数 (20 分)
阅读量:705 次
发布时间:2019-03-21

本文共 1137 字,大约阅读时间需要 3 分钟。

技术员笔记:针对这个问题,我决定使用埃拉托斯特尼筛法来生成素数列表,然后从指定的位置提取所需的素数。以下是优化后的解决方案:

  • 筛法生成素数:创建一个布尔数组,用来标记每个数是否为素数。初始化时将0和1标记为非素数。然后,从2开始遍历,标记它们的倍数。

  • 收集素数列表:在筛法执行过程中,遍历每个数,若其为素数,则将其加入素数列表中。

  • 读取输入:从标准输入读取M和N的值,确保M≤N。

  • 输出结果:遍历素数列表,从M的位置到N的位置,输出每10个数一行,格式为"p p p...",不带多余空格。

  • 代码实现:这段代码使用了C++语言,并采用简洁的方法实现了以上功能。为了确保程序高效运行,主执行部分使用了while循环,而非传统的for循环。

    #include 
    #include
    using namespace std;vector
    sieve(int size) { vector
    prime(size, true); prime[0] = prime[1] = false; for (int i = 2; i * i <= size; ++i) { if (prime[i]) { for (int j = i * i; j < size; j += i) { prime[j] = false; } } } vector
    primes; for (int i = 0; i < size; ++i) { if (prime[i]) { primes.push_back(i); } } return primes;}int main() { int m, n; vector
    primes = sieve(1000000); for (int i = 0; i < primes.size(); ++i) { if (i == m -1) { if (m <= n) { // proceed to read M and N } } } // reading M and N here...}

    此代码先生成足够大的素数列表,然后读取输入M和N,最后输出对应的素数。由于问题描述中没有涉及一线输入处理的具体细节,所以这部分需要根据实际情况调整。

    转载地址:http://omyrz.baihongyu.com/

    你可能感兴趣的文章
    nginx+Tomcat性能监控
    查看>>
    nginx+uwsgi+django
    查看>>
    nginx+vsftp搭建图片服务器
    查看>>
    Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
    查看>>
    nginx-vts + prometheus 监控nginx
    查看>>
    Nginx/Apache反向代理
    查看>>
    Nginx: 413 – Request Entity Too Large Error and Solution
    查看>>
    nginx: [emerg] getpwnam(“www”) failed 错误处理方法
    查看>>
    nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
    查看>>
    nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
    查看>>
    nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
    查看>>
    Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
    查看>>
    nginxWebUI runCmd RCE漏洞复现
    查看>>
    nginx_rtmp
    查看>>
    Vue中向js中传递参数并在js中定义对象并转换参数
    查看>>
    Nginx、HAProxy、LVS
    查看>>
    nginx一些重要配置说明
    查看>>
    Nginx一网打尽:动静分离、压缩、缓存、黑白名单、跨域、高可用、性能优化......
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    Nginx与Tengine安装和使用以及配置健康节点检测
    查看>>