函数题:编写函数判断素数
要求实现一个函数,能够高效判断一个非负整数$n(1<=n<2^{31})$是否素数。
### 函数接口定义:
c++
bool isPrime(int n);
其中 n 是用户传入的参数,存放待判断的整数。
### 裁判测试程序样例:
c++
#include<iostream>
#include<cmath>
using namespace std;
//判断输入的正整数n是否素数,是则输出yes,否则输出no
int main() {
int n;
while(cin>>n) {
if(isPrime(n)==true)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
### 输入样例:
in
101
1001
### 输出样例:
out
yes
no
答案:若无答案欢迎评论
### 函数接口定义:
c++
bool isPrime(int n);
其中 n 是用户传入的参数,存放待判断的整数。
### 裁判测试程序样例:
c++
#include<iostream>
#include<cmath>
using namespace std;
//判断输入的正整数n是否素数,是则输出yes,否则输出no
int main() {
int n;
while(cin>>n) {
if(isPrime(n)==true)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
### 输入样例:
in
101
1001
### 输出样例:
out
yes
no
答案:若无答案欢迎评论