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 (≤108 10^810
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

【声明】

  由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。

Solution:

  这道题很简单,就是判断一下n 以及 n-6 或者 n+6是素数,不是的话,那就n++,直到满足要求

  

 #include <iostream>
using namespace std;
bool isPrime(int x)//判断是不是素数
{
if (x < )
return x >= ;
for (int i = ; i*i <= x; ++i)
if (x%i == )
return false;
return true;
}
int isSexyPrimes(int x)//判断满不满足题目要求
{
if (isPrime(x))
{
bool fL = isPrime(x - ), fR = isPrime(x + );
if (fL || fR)
return fL ? x - : x + ;
}
return -;
}
int main()
{
int n;
cin >> n;
int res = isSexyPrimes(n);
if (res > )
{
printf("Yes\n");
printf("%d\n", res);
}
else
{
printf("No\n");
for (int i = n + ; i < INT32_MAX; ++i)//另寻找大数,直至满足要求
{
if (isSexyPrimes(i) > )
{
printf("%d\n", i);
break;
}
}
}
return ;
}

PAT甲级【2019年3月考题】——A1156 SexyPrimes【20】的更多相关文章

  1. PAT甲级【2019年3月考题】——A1157 Anniversary【25】

    Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...

  2. PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】

    7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...

  3. PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】

    7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...

  4. PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】

    7-2 Merging Linked Lists (25 分) Given two singly linked lists L 1 =a 1 →a 2 →...→a n−1 →a n  L1=a1→a ...

  5. PAT甲级【2019年9月考题】——A1160 Forever【20】

    7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...

  6. PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【30】

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

    Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...

  8. PAT甲级2019冬季考试题解

    A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...

  9. 【PAT甲级】1116 Come on! Let's C (20分)

    题意: 输入一个正整数N(<=10000),接着依次输入N个学生的ID.输入一个正整数Q,接着询问Q次,每次输入一个学生的ID,如果这个学生的ID不出现在之前的排行榜上输出Are you kid ...

随机推荐

  1. 【洛谷p1217】回文质数

    回文质数[题目链接] 始终要记得凌云壮flag(真香) 说是个搜索,其实感觉更像是一个暴力: 这个题的难度并不是特别大,因为下面的提示太明显了qwq,(而且之前培训也讲过)首先是构造回文数,构造回文数 ...

  2. 13、numpy——算术函数

    NumPy 算术函数 1.NumPy 算术函数包含简单的加减乘除: add(),subtract(),multiply() 和 divide(). 需要注意的是数组必须具有相同的形状或符合数组广播规则 ...

  3. sudo在清理内存的时候报错

    运行下面语句清缓存时,报Permission denied错误:-bash: /proc/sys/vm/drop_caches: Permission denied sudo echo 1 > ...

  4. Css3-文字

    一.text-overflow   text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法:text-overflow:clip(默认属性,表示剪切) | ell ...

  5. mpvue中的平台状态判断(H5网页 or 小程序)

    在开发微信小程序或者微信网页H5的时候,有时我们利用外部组件可能不兼容这两者,需要区分开来,可以在对应的mainjs中配置如下 let platform: try{ if(wx){ platform= ...

  6. 修改Oracle并行度

    什么是并行度: 并行度的优点就是能够最大限度的利用机器的多个cpu资源,是多个cpu同时工作,从而达到提高数据库工作效率的目的.在系统空闲时间,使用并行是个不错的选择,但是好东西总是相对而言,没有绝对 ...

  7. webRTC脱坑笔记(三)— webRTC API之RTCPeerConnection

    RTCPeerConnection API是每个浏览器之间点对点连接的核心,RTCPeerConnection是WebRTC组件,用于处理对等体之间流数据的稳定和有效通信. RTCPeerConnec ...

  8. JS 判断undefined

    tax !== underfined underfined 是判断的是类型的结果, 如果加typeof后是字符串类型 写法:typeof(tax) !== "underfined" ...

  9. Nmon监控服务端性能

    一.安装1.查看服务器操作系统的版本信息 lsb_release -a cat /etc/*release2.下载 a.nmon下载地址:http://nmon.sourceforge.net/pmw ...

  10. SQL Server 2005 的动态管理视图DMV和函数DMF

    优化 的动态管理视图DMV和函数DMF SQL Server 05提供了动态管理视图Dynamic Management Views和函数 Functions,方便了我们对系统运行情况的监控,故障诊断 ...