单点时限: 2.0 sec
内存限制: 256 MB
Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if given number is nearly prime and “No” otherwise.
输入格式
Input file consists of N+1 numbers. First is positive integer N (1<=N<=10). Next N numbers followed by N. Each number is not greater than 10^9. All numbers separated by whitespace(s).
输出格式
Write a line in output file for each number of given sequence. Write “Yes” in it if given number is nearly prime and “No” in other case.
样例
input
1
6
output
Yes
题意:判断一个数是否为两个质数的乘积
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
bool isr(int n) {
if(n<2)
return false;
int m=sqrt(n);
for(int i =2; i <= m; i++)
if(n%i==0)
return false;
return true;
}
void f(int n) {
if(n%2==0) {
if(isr(n/2))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return;
}
for(int i =3; i <=sqrt(n); i++) {
if(n%i==0) {
if(isr(i)&&isr(n/i)) {
cout<<"Yes"<<endl;
return;
}
}
}
cout<<"No"<<endl;
}
int main() {
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
f(n);
}
return 0;
}