PAT_B_1013 数素数 (20 分)【格式错误问题】【某个测试点未通过问题】

令 P​i​​ 表示第 i 个素数。现任给两个正整数 M≤N≤10​^4​​,请输出 P​M​​ 到 P​N​​ 的所有素数。

输入格式:

输入在一行中给出 M 和 N,其间以空格分隔。

输出格式:

输出从 P​M​​ 到 P​N​​ 的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。

输入样例:

5 27

输出样例:

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 <math.h>
using namespace std;
bool sushu(int n);
int main()
{
    int m,n;
    cin>>m>>n;
    int count=0;
    int row=1;
    int i=2;
    while(count<=n)      //开始的时候用的是for 循环遍历 10^4内的素数,题目理解问题
    {
        if(sushu(i))
        {
            count++;
            if(count>=m)
            {
                if(count>n) break;
                if(count<n)          //最后一个数之后也不能有空格
                {
                    if(row%10)
                    {
                        cout<<i<<" ";
                    }else
                    {
                        cout<<i<<endl;
                    }
                    ++row;
                }else{
                    cout<<i;
                }
            }
        }
        ++i;
    }
    return 0;
}
bool sushu(int n)
{
    int nn=sqrt(n);
    for(int i=2;i<=nn;i++)
    {
        if(n%i==0) return false;
    }
    return true;
}
 

猜你喜欢

转载自blog.csdn.net/id33749110/article/details/85642032
今日推荐