40分求条全是TLE
查看原帖
40分求条全是TLE
1433392
Konosekai楼主2024/11/22 19:44
#include<iostream>
#include<cmath>
using namespace std;
bool isPrime(int num){
    if(num<=1)
		return false;
	int a=sqrt(num);
    for(int i=2;i<=a;i++){
        if (num%i==0) 
			return false;
	}
    return true;
}
int countPrimesBetween(int a,int b){
    int count=0;
    if(a>b)
        swap(a,b);
    for(int i=a;i<=b;i++){
        if(isPrime(i))
            count++;
    }
    return count;
}
int main(){
    int num1,num2;
    cin>>num1>>num2;
    int primeCount=countPrimesBetween(num1,num2);
    cout<<primeCount<<endl;
    return 0;
}
2024/11/22 19:44
加载中...