题解 | 素数判断
素数判断
https://www.nowcoder.com/practice/5ab1b9690af047699e96c87dee65def4
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool isprime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int n = 0;
int time = 0;
scanf("%d", &time);
while (time--)
{
scanf("%d", &n);
if (isprime(n))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
查看2道真题和解析