PAT求素数

令Pi表示第i个素数。现任给两个正整数M <= N <= 10000,请输出PM到PN的所有素数。

#include<iostream>
#include<stdio.h>
using namespace std;

int main() {
    int M, N, i=0,j=0,p=0;
    int a[104743] = { 0 }, b[10002] = { 0 };

    cin >> M >> N;

    while (i == 0 || i == 1) {
        a[i] = 0;
        i++;
    }

    for (i = 2; i <= 104743; i++) {
        if (a[i] == 1) {
            continue;
        }
        for (j = i*i; j <= 104743; j = j + i) {
            a[j] = 1;
        }
    }

    for (i = 0; i <= 104743; i++) {
        if (a[i] == 0) {
            b[p] = i;
            p++;
        }
        else
            continue;
    }

    int m=0;
    for (p = M+1; p <= N+1; p++) {
        cout << b[p];
        m++;
        if (m % 10 != 0 && p!=N+1)
            cout << " ";
        if (m % 10 == 0)
            cout << ";"<< "\n";
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42082542/article/details/83155217
今日推荐