为什么排除偶数后第三个测试点过不了,删除偶数判断后能过
查看原帖
为什么排除偶数后第三个测试点过不了,删除偶数判断后能过
790884
Zxxcme楼主2024/7/25 15:47
#include <bits/stdc++.h>
using namespace std;

bool primary(int i)  //判断是否为素数
{
    for(int j=2;j<=sqrt(i);j++)
    {
        if(i%j==0)  
            return 0;
    }
    return 1;
}

int main()
{
    int X,Y;
    cin>>X>>Y;
    if(X>Y)
        swap(X,Y);
    int count=0;
    for(int i=X;i<=Y;i++)
    {
        if(i==1||i%2==0)    //排除1的特殊情况,还有2的倍数减少程序运行时间
            continue;
        else if(primary(i))
            count++;
    }
    cout<<count;
    return 0;
}
2024/7/25 15:47
加载中...