题解 | 素数判断
素数判断
https://www.nowcoder.com/practice/5ab1b9690af047699e96c87dee65def4
#include<bits/stdc++.h>
using namespace std;
bool foo (int num){
if (num <=1){
return false;
}
if (num == 2){
return true;
}
if (num%2==0){
return false;
}
for (int i =3;i<sqrt(num);i +=2){
if(num%i==0){
return false;
}
}
return true;
}
int main(){
int T;
cin >> T;
int a[T];
for(int i =0;i<T;i++){
cin >>a[i];
if(foo(a[i])){
cout <<"Yes"<<endl;
}
else{
cout <<"No"<<endl;
}
}
return 0;
}
bool 函数用来筛选

查看27道真题和解析