• PAT 甲级 1156 Sexy Primes (20分)


    Sexy primes are pairs of primes of the form (p, p+6), so-named since "sex" is the Latin word for "six". (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)

    Now given an integer, you are supposed to tell if it is a sexy prime.

    Input Specification:

    Each input file contains one test case. Each case gives a positive integer N (<=10^8).

    Output Specification:

    For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, print No instead, then print in the next line the smallest sexy prime which is larger than N.

    Sample Input 1:

    47
    

    Sample Output 1:

    Yes
    41
    

    Sample Input 2:

    21
    

    Sample Output 2:

    No
    23

    【AC代码】简单素数判断—不能再简单了~

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 bool isPrime(int x)
     5 {
     6     if(x < 2) return false;
     7     for(int i = 2; i*i <= x; i++)
     8         if(x % i == 0) return false;
     9     return true;
    10 }
    11 
    12 int main()
    13 {
    14     int n; cin >> n;
    15     if(isPrime(n))
    16     {
    17         if(isPrime(n-6)) 
    18         {
    19             cout << "Yes" << endl << n-6;
    20             return 0;
    21         }
    22         else if(isPrime(n+6))
    23         {
    24             cout << "Yes" << endl << n+6;
    25             return 0;
    26         }
    27     }
    28      else{
    29          while(1)
    30          {
    31              n+=1;
    32              if(isPrime(n) && isPrime(n+6))
    33              {
    34                  cout << "No" << endl << n; return 0;    
    35             }
    36             if(isPrime(n) && isPrime(n-6))
    37              {
    38                  cout << "No" << endl << n; return 0;    
    39             }
    40         }
    41      }
    42  } 
    View Code
  • 相关阅读:
    django项目环境搭建备忘
    Python IDE的选择和安装
    MAC上python环境搭建
    hadoop1.2.1+hbase0.90.4+nutch2.2.1+elasticsearch0.90.5配置(伪分布式)
    ubuntu下hadoop完全分布式部署
    ubuntu下集群设置静态ip
    C语言调用库函数实现生产者消费者问题
    poj 1703(带权并查集)
    poj 1330
    poj1724
  • 原文地址:https://www.cnblogs.com/kamisamalz/p/13598755.html
Copyright © 2020-2023  润新知