题解 | #素数判定#

素数判定

https://www.nowcoder.com/practice/5fd9c28b1ce746dd99287a04d8fa9002

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

//例题6.7 素数判定 
int isPrime(int num) {
    if (num == 1) {
        return 0;
    }
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int n;
    while (cin >> n) {
        if (isPrime(n) == 1) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }

    return 0;
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务