Description
Prime Number Definition
An integer greater than one is called a prime number if its only positive divisors (factors) are one and itself. For instance, 2, 11, 67, 89 are prime numbers but 8, 20, 27 are not.
Semi-Prime Number Definition
An integer greater than one is called a semi-prime number if it can be decompounded to TWO prime numbers. For example, 6 is a semi-prime number but 12 is not.
Your task is just to determinate whether a given number is a semi-prime number.
Input
There are several test cases in the input. Each case contains a single integer N (2 <= N <= 1,000,000)
Output
One line with a single integer for each case. If the number is a semi-prime number, then output "Yes", otherwise "No".
Sample Input
3
4
6
12
Sample Output
Yes
Yes
No
判断是否是半素数,半素数就是两个素数的乘积
分析:
先找出1000以前的素数,然后判断n/素数是否为素数
代码:
#include<bits/stdc++.h>
using namespace std;
int vp(long int n)
{
int i,f;
for(i=2,f=0;i<=sqrt(n);i++)
if(n%i==0)
{
f=1;
break;
}
if(f)
return 0;
else
return 1;
}
int main()
{
long int i,n,k,a[1500],f,t;
for(i=2,t=0;i<1000;i++)
if(vp(i)){
a[t]=i;
t++;
}
while(cin>>n)
{
for(i=0,f=0;i<t;i++){
if(n%a[i]==0&&vp(n/a[i])&&n>a[i])
{
f=1;
break;
}
if(n<=a[i])
break;
}
if(f)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
感觉:
我感觉我的方法有点粗暴。。不太好