Files
python/TangDou/AcWing/Prime/866.md
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

58 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##[$AcWing$ $866$. 试除法判定质数](https://www.acwing.com/problem/content/description/868/)
### 一、题目描述
给定 $n$ 个正整数 $a_i$,判定每个数是否是质数。
**输入格式**
第一行包含整数 $n$。
接下来 $n$ 行,每行包含一个正整数 $a_i$。
**输出格式**
共 $n$ 行,其中第 $i$ 行输出第 $i$ 个正整数 $a_i$ 是否为质数,是则输出 `Yes`,否则输出 `No`。
**数据范围**
$1≤n≤100,1≤a_i≤2^{31}−1$
**输入样例:**
```cpp {.line-numbers}
2
2
6
```
**输出样例:**
```cpp {.line-numbers}
Yes
No
```
### 二、实现代码
```cpp {.line-numbers}
#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;
}
```