题解 | #回文数判断#

判断一个数是不是质数

http://www.nowcoder.com/practice/b8bb5e7703da4a83ac7754c0f3d45a82

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int);

int main() {

    // write your code here......
    int x;
    cin>>x;
    
    if(isPrime(x)) cout<<"是质数";
    else cout<<"不是质数";

    return 0;
}

// 开根法判断素数,固定格式,背熟了就完事了
bool isPrime(int x){
    if(x<=2) return true;
    for(int i=2;i<=sqrt(x);i++){
        if(x%i==0) return false;
    }
    return true;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务