PAT B Zhenti 1003. The Primes

PAT B Zhenti 1003. The Primes

Title Description

Pi denotes the i-th order prime number. The current to the two positive integers M <= N <= 10000, please PM to output PN of all primes.

Input Format

M and N are given in the input line, separated by a space therebetween.

Output Format

PM outputted from the PN to 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

Topic ideas

Note that behind each row can not have spaces

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int q[N], m, n, i = 0;

void pri_num(int n)
{
    int k = 2;
    while (i < n)
    {
        int j = 2;
        for (; j <= sqrt(k) + 1; j++)
        {
            if (k%j == 0)break;

        }
        int t = sqrt(k) + 1;//sqrt(k)+1是一个浮点数,所以需要转换一下。
        if (j >= t)
        {
            q[i++] = k;
        }
        k++;
    }
}

int main()
{
    scanf("%d%d",&m,&n);
    pri_num(n);
    for (int j = m - 1; j < i; j++)
    {
        if ((j - (m - 1)+ 1) % 10 == 0)
        {
            printf("%d\n", q[j]);
        }
        else if(j == i - 1)
        {
            printf("%d", q[j]);
        }    
        else
        {
            printf("%d ", q[j]);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fsh001/p/12186856.html