'commit'
This commit is contained in:
58
TangDou/AcWing/Prime/866.md
Normal file
58
TangDou/AcWing/Prime/866.md
Normal file
@@ -0,0 +1,58 @@
|
||||
##[$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;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user