ACM程序设计题目 Problem R-18

来源:互联网 发布:mac 简体中文输入法 编辑:程序博客网 时间:2024/06/04 21:54

//就是判断一个数是不是半素数。半素数就是只有1和其他两个素数因子,题目意思很简单,但是容易超时,这个提我提交了11次,都是超时下面是通过的代码。
//思路就是先把素数找出来,,存到数组里,然后将他们1000000以内的乘积都放入一个容器中,输入n后直接查表就行了。
#include <bits/stdc++.h>using namespace std; vector<int> v;set<int> s; void pt(int a,int b) {  for(int i=a;i<=b;i++)  {  if(i!=2 && i%2==0)continue;  for(int j=3;j*j<=i;j+=2)  {  if(i%j==0)goto RL;  }  v.push_back(i); RL:continue;  } } int main() {  pt(2,500000);  int i,j,p,n; set<int>::iterator it;  for(i=0;i<v.size();i++)  {  for(j=0;j<v.size();j++)  {  p=v[i]*v[j];  if(p<1000000)  s.insert(p);  else  break;  }  }   while(cin>>n)  {  it=s.find(n);  if(it!=s.end())  cout<<"Yes"<<endl;  else  cout<<"No"<<endl;  }  return 0; }
//这是通过了的代码,我之前的思路是把素数筛选出来之后存进去,然后查找n的因子,再判断这两个数是不是素数。超时的原因应该是我晒素数的方法慢。。。
#include <bits/stdc++.h>using namespace std;set<int>s;void shai(){int n=10000,i,j;s.insert(2);s.insert(3);s.insert(5);for(i=5;i<n;i+=2){if(i%3==0) continue;if(i%5==0) continue;for(j=2;j<i;j++){if(i%j==0) break;if(j==i-1) s.insert(i);}}}int main(){int i,j,n,p,b;set<int>::iterator x;set<int>::iterator y;shai();while(cin>>n){p=0;for(i=2;i<=n;i++)for(j=1;j<n;j++){if(i*j==n){x=s.find(i);y=s.find(j);if(x!=s.end()){if(y!=s.end()){p=1;goto R;}}}}R:{if(p==1)  cout<<"Yes"<<endl;if(p==0)  cout<<"No"<<endl;}}    return 0;}
//到网上看了几种晒素数的方法,是判断后存0或1代表是不是素数。

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

No 
Yes 
Yes 
No 

0 0
原创粉丝点击