PAT甲级【2019年3月考题】——A1156 SexyPrimes【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 (≤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】的更多相关文章
- PAT甲级【2019年3月考题】——A1157 Anniversary【25】
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebrat ...
- PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】
7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...
- PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】
7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...
- 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 ...
- PAT甲级【2019年9月考题】——A1160 Forever【20】
7-1 Forever (20 分) "Forever number" is a positive integer A with K digits, satisfying the ...
- 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 ...
- PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】
Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...
- PAT甲级2019冬季考试题解
A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...
- 【PAT甲级】1116 Come on! Let's C (20分)
题意: 输入一个正整数N(<=10000),接着依次输入N个学生的ID.输入一个正整数Q,接着询问Q次,每次输入一个学生的ID,如果这个学生的ID不出现在之前的排行榜上输出Are you kid ...
随机推荐
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- python学习第十三天元组创建和操作方法
有人问,有了列表,为什么还要有元组呢,到底元组是什么,元组是不可变的有序的列表,一旦创建不能改变,那些地方用到元组呢,小编知道可以应用到数据库连接. 1,元组的创建 n1 = () 元组用的是小括号 ...
- 38.0到n-1中缺失的数字
题目描述: 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字的范围都在0到n-1之内.在范围0到n-1内的n个数字中有且仅有一个数字不在该数组中,求出该数字. 思路分析: 因 ...
- Debian(Linux)+XAMPP(LAMPP)+Zend Studio + PHP +XDebug 完整的开发环境配置方法。 转摘:http://www.cnblogs.com/kungfupanda/archive/2010/11/25/1887812.html
经历了3天左右的挣扎,终于在Linux下将 php开发工具 Zend Studio 的 xdebug安装成功,分享如下: 1,装XAMPP,安装方法链接如下:这里假设XAMPP的安装路径为:/opt/ ...
- 并行开发 2.task
原文:8天玩转并行开发——第二天 Task的使用 在我们了解Task之前,如果我们要使用多核的功能可能就会自己来开线程,然而这种线程模型在.net 4.0之后被一种称为基于 “任务的编程模型”所冲击, ...
- 设置第一个以外的元素样式:not(:first-child),设置最后一个除外的元素样式:not(:last-child)
代码截图:
- 使用HystrixCommand封装http请求
1.引入依赖 要排除hystrix-core里的archaius-core,否则报错 <dependency> <groupId>com.netflix.hystrix< ...
- ahocorasick使用
一.作用 字符串匹配,比如现在有个大的列表,客户输入一句话,如何根据客户输入的一句话,从大列表中匹配出字符串交集 具体请详细查阅 二.示例 比如我们有一个wordlist列表,长度很长,包含43430 ...
- 让docker容器使用主机系统时间(挂入/etc/localtime)
-v挂入这个文件就可以了: -v /etc/localtime:/etc/localtime:ro
- springboot全局字符编码设置
1.在application.properties中设置 #编码格式 spring.http.encoding.force=true spring.http.encoding.charset=UTF- ...