PAT Basic 1013 number prime numbers (20 minutes)

So that  P i represents  the i-th prime number. The current to the two positive integers  M N . 1 0 . 4 , please output  P M to  P N all prime numbers.

Input formats:

In the given input row  M and  N, separated by a space therebetween.

Output formats:

Output from the  P M to  P N all prime numbers, each representing a digit line 10, separated by spaces therebetween, the end of the line may not have extra space.

Sample input:

5 27

Sample output:

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 <cmath>
#include <vector>
using namespace std;
bool isPrime(int n){
    if(n==2||n==3) return true;
    for(int i=2;i<=sqrt(n);i++){
        if(n%i==0) return false;
    }
    return true;
}
int main(){
    int l,r,res=0,br=0;
    cin>>l>>r;
    for(int i=2;res<=r;i++){
        if(isPrime(i)){
            res++;
            if(res>=l&&res<=r){
                cout<<i;br++;
                if(br%10==0||res==r) cout<<endl;
                else cout<<" ";
            }
        }
    }
    system("pause");
    return 0;
}

 




Guess you like

Origin www.cnblogs.com/littlepage/p/11372114.html