题解 | #素数判定#

素数判定

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

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

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        // cout << a + b << endl;
        if (n <= 0 || n == 1) {
            cout << "no" << endl;
            continue;
        }
        int bound = sqrt(n);
        bool flag = true;
        for (int i = 2; i <= bound; i++) {
            if (n % i == 0) {
                flag = false;
                break;
            }

        }
        if (flag == false)cout << "no" << endl;
        else cout << "yes" << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务