PAT 2019-3 7-1 Sexy Primes
Description:
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 (≤).
Output Specification:
For each case, print in a line
Yesif 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, printNoinstead, 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
Keys:
- 素数
Attention:
- 这几次很爱考素数,奶一口欧拉算法,9月份考试能不能中
Code:
/*
SP数,P和P+6都是素数
给定一个正整数,判断N是否为SP数,打印与他配对的另一个素数(如果不唯一,输出较小的一个
如果N不是SP数,打印大于N的最小SP数 1,判断i-6是否大于n
2,n-6可能小于0,
*/
#include<cstdio> bool IsPrime(long long n)
{
if(n <= )
return false;
for(long long i=; i*i<=n; i++)
if(n%i == )
return false;
return true;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDEG int n;
scanf("%d",&n);
if(IsPrime(n-) && IsPrime(n))
printf("Yes\n%d", n-);
else if(IsPrime(n) && IsPrime(n+))
printf("Yes\n%d", n+);
else
{
for(int i=n+; i<1e9; i++)
if(IsPrime(i-) && IsPrime(i))
{
printf("No\n%d", i->n?i-:i);
break;
}
} return ;
}
PAT 2019-3 7-1 Sexy Primes的更多相关文章
- PAT 2019 春
算是第二次做这套题吧,感觉从上次考试到现在自己有了挺大提高,提前30min做完了. 7-1 Sexy Primes 读懂题意就行. #include <cstdio> #include & ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT 2019 秋
考试的还行.不过略微有点遗憾,但是没想到第一题会直接上排序和dfs,感觉这次的题目难度好像是倒着的一样.哈哈哈哈. 第一题实在是搞崩心态,这道题给我的感觉是,可以做,但事实上总是差点啥. 第二题,第三 ...
- PAT甲题题解-1015. Reversible Primes (20)-素数
先判断n是否为素数然后把n转化成d进制下再反转,转化为十进制的num判断num是否为素数 注意n为0和1时,不是素数!!!注意反转后的num也有可能为1,不是素数!!! #include <io ...
- 【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- 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 “si ...
- PAT(甲级)2019年春季考试
7-1 Sexy Primes 判断素数 一个点没过17/20分 错因:输出i-6写成了输出i,当时写的很乱,可以参考其他人的写法 #include<bits/stdc++.h> usin ...
- 三月pat(转)
转自https://blog.csdn.net/weixin_40688413/article/details/88082779 担心别人删除了就找不到了.因为九月要考. 7-1 Sexy Prime ...
随机推荐
- 前端最常用的跨域方式--jsonp
jsonp通过动态创建script标签的方式来实现跨域通信.原理是浏览器允许html标签在不同的域名下加载资源. <script> var script = document.create ...
- Codeforces 1162E Thanos Nim(博弈)
一道有意思的博弈题.首先我们考虑一种必败情况,那就是有一方拿光了一堆石子,显然对方是必胜,此时对方可以全部拿走其中的n/2,那么轮到自己时就没有n/2堆,所以此时是必败态.我们先对所有石子堆sort, ...
- Java的socket编程中关于bufferedWriter的发送问题
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String send ...
- 全文搜索引擎 elasticsearch.net
原文:https://www.cnblogs.com/lonelyxmas/p/10767436.html
- BUUCTF--xor
测试文件:https://buuoj.cn/files/caa0fdad8f67a3115e11dc722bb9bba7/7ea34089-68ff-4bb7-8e96-92094285dfe9.zi ...
- ios-实现ARC与MRC混编
选择target -> build phases -> compile sources -> 用ARC的文件将compiler flags设置为:-fobjc-arc,用MRC的文件 ...
- xcode 5.0 连接svn error -(NSURLErrorDomain error -1012)
xcode 5.0连接 svn server, check out时出现如下error : The operation couldn’t be completed. (NSURLErrorDomain ...
- HTML基础 用div布局实现一个简单网页
div布局如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- 同步与异步,阻塞与非阻塞 bio,nio,aio
BIO.NIO和AIO的区别(简明版) 同步异步,阻塞非阻塞: https://www.zhihu.com/question/19732473 转载请注明原文地址:http://www.cnblo ...
- [BZOJ5305] [HAOI2018] 苹果树 数学 组合计数
Summary 题意很清楚: 小 \(C\) 在自己家的花园里种了一棵苹果树, 树上每个结点都有恰好两个分支. 经过细心的观察, 小 \(C\) 发现每一天这棵树都会生长出一个新的结点. 第一天的时候 ...