pat level B——1013 output the specified prime number (it is worth learning the output and the method of judging prime numbers)

给两个数例如2,27
则输出第二个素数到第27个素数

每十个换行,切两个之间有空格
最后一个数后无空格
值得学习
#include<stdio.h>
#include<math.h>

int IsPrime(int b);    //判断是否为素数

int main()
{
    int M, N, a = 0, b = 2, count = 0;
    scanf("%d %d", &M, &N);
    while(a < N){
        if(IsPrime(b)){
            a++;    //a用来记录现在是第几个素数
            if(a >= M){
                printf("%d", b);
                count++;    //count用来记录已经输出多少个素数
                if(count % 10 == 0){
                    printf("\n");
                }
                else if(a != N){
                    printf(" ");
                }
            }
        }
        b++;
    }
}

int IsPrime(int b){
    int flag = 1;
    for(int i = 2; i <= sqrt(b); i++){
        if(b % i == 0){
            flag = 0;
            break;
        }
    }
    return flag;
}

Guess you like

Origin blog.csdn.net/weixin_45663946/article/details/109223886