zoukankan      html  css  js  c++  java
  • zoj 2723 Semi-Prime(set)

    Semi-Prime

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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

    建立素数表和半素数表,建立半素数表是通过素数表中的任意两个素数相乘得到保存起来。

     1 #include <iostream>
     2 #include <vector>
     3 #include <set>
     4 #include <cmath>
     5 using namespace std;
     6 //建立全局向量,用来保存素数
     7 vector<int> v;
     8 //在全局内存中定义全局集合容器,用来保存半素数
     9 //集合是平衡二叉检索树,搜索速度最快
    10 set<int> s;
    11 //建立[a, b]范围内素数表
    12 void pt(int a, int b){
    13     for(int i = a; i <= b; i++){
    14         //2是素数,清除2的倍数
    15         if(i != 2 && i % 2 == 0) continue;
    16         //消除素数的倍数 
    17         for(int j = 3; j * j <= i; j += 2){
    18             if(i % j == 0)
    19                 goto RL;
    20         }
    21         v.push_back(i);
    22         RL: continue;
    23     }
    24 }
    25 
    26 int main(){
    27     pt(2, 500000);
    28     int i, j, p;
    29     for(i = 0; i < v.size(); i++){
    30         for(j = 0; j < v.size(); j++){
    31             p = v[i] * v[j];
    32             if(p < 1000000)
    33                 s.insert(p);
    34             else
    35                 break;
    36         }
    37     }
    38     //读入数据,在半素数表中查找,看是否在该表
    39     int n;
    40     set<int>::iterator it;
    41     while(cin >> n){
    42         it = s.find(n);
    43         if(it != s.end())
    44             cout << "Yes" << endl;
    45         else
    46             cout << "No" << endl;
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    从Python到Web开发
    源码安装缺少configure文件
    5
    4
    3
    2
    42
    1
    18
    41
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/6530865.html
Copyright © 2011-2022 走看看