1013. 数素数 (20)——做题纪录

1013. 数素数 (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

令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

//
//  main.cpp
//  PATBase1013
//
//  Created by Tong on 4/12/16.
//  Copyright © 2016 Tong. All rights reserved.
//

#include <iostream>
#include <math.h>

using namespace std;

bool ifPrime(int n){
    for (int i = 2; i <= sqrt(n); i++) {
        if (n%i==0) {
            return false;
        }
    }
    return true;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    
    int M, N, count = 0;
    
    cin>>M>>N;

    for (int i = 2; ; i++) {
        int n;
        if (n>10) {
            int n = i % 10;
            if (n!=1 && n!=3 && n!=7 &&n !=9) {
                continue;
            }
        }
        if (ifPrime(i)) {
            count++;
            if (count>=M) {
                cout<<i;
                if (count==N) {
                    break;
                }else{
                    if ((count-M+1)%10!=0) {
                        cout<<" ";
                    }else{
                        cout<<endl;
                    }
                }
            }
        }
    }
    
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值