试除法判断质数
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[120];
bool isp(int x)
{
if(x<2)return false;
for(int i=2;i<=x/i;i++)
{
if(x%i==0)
{
return false;
}
}
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(isp(a[i]))
{
cout<<"Yes\n";
}
else
{
cout<<"No\n";
}
}
return 0;
}

浙公网安备 33010602011771号