PAT1013 数素数

思路: 打印素数表 然后找出对应区间[m,n]中的素数

#include <iostream>  
#include <vector>  
#include <cmath>  
using namespace std;  
  
vector<int> v;  
  
bool IsSu(int x)  //判断是否是素数
{  
    for(int i = 2; i <= sqrt(x); ++i)  
        if((x%i)==0)  
            return false;  
    return true;  
}  
  
int main()  
{  
    int M, N, i = 2;  
    while(1)  
    {  
        if(v.size() > 10000)  
            break;  
        if(IsSu(i))  
            v.push_back(i);  
        i++;  
    }  
    cin>>M>>N;  
    int j = 1;  
    for(i = M-1; i < N; ++i)  
    {  
        if(!(j%10) || i == N-1)  
            cout<<v[i]<<endl;  
        else  
            cout<<v[i]<<" ";  
        j++;  
    }  
    return 0;  
}  
原文地址:https://www.cnblogs.com/wshyj/p/6288951.html