You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

##AcWing 866. 试除法判定质数

一、题目描述

给定 n 个正整数 a_i,判定每个数是否是质数。

输入格式 第一行包含整数 n

接下来 n 行,每行包含一个正整数 a_i

输出格式n 行,其中第 i 行输出第 i 个正整数 a_i 是否为质数,是则输出 Yes,否则输出 No

数据范围 1≤n≤100,1≤a_i≤2^{31}1

输入样例:

2
2
6

输出样例:

Yes
No

二、实现代码

#include <bits/stdc++.h>

using namespace std;

bool isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i <= n / i; i++)
        if (n % i == 0) return false;
    return true;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        int a;
        cin >> a;
        if (isPrime(a))
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}