Information
- ID
- 43
- Time
- 5000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 1287
- Accepted
- 262
- Uploaded By
#include<stdio.h>
//判断质数 一个质数的因子只有1和它本身
bool is_prime(int num) {
if (num <= 1) return false; // 小于等于1的数不是质数
if (num == 2) return true; // 2是质数
if (num % 2 == 0) return false; // 排除偶数
for (int i = 3; i * i <= num; i += 2) {
if (num % i == 0) {
return false; // 如果能被除了1和自身以外的数整除,则不是质数
}
}
return true; // 上述条件都不满足,则为质数
}
int main() {
int n,answer;
scanf("%d",&n);
while (n--){
int x;
scanf("%d",&x);
answer=is_prime(x);
if (answer){
printf("Yes\n");
} else{
printf("No\n");
}
}
return 0;
}
```
By signing up a 追梦算法网 universal account, you can submit code and join discussions in all online judging services provided by us.