1156 Sexy Primes (20 point(s))

素数

题目

“Forever number” is a positive integer A with K digits, satisfying the following constrains:

the sum of all the digits of A is m;
the sum of all the digits of A+1 is n; and
the greatest common divisor of m and n is a prime number which is greater than 2.
Now you are supposed to find these forever numbers.

Input Specification:
Each input file contains one test case. For each test case, the first line contains a positive integer N(≤5)N (≤5)N(≤5). Then N lines follow, each gives a pair of K(3<K<10)K (3<K<10)K(3<K<10) and m(1<m<90)m (1<m<90)m(1<m<90), of which the meanings are given in the problem description.

Output Specification:
For each pair of K and m, first print in a line Case X, where X is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution.

Sample Input:

2
6 45
7 80

Sample Output:

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)N (≤10^8)N(≤108).

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

思路

题意

给出一个数字,判断其是否是SexyPrime(即该数n和(数n-6或n+6)都是素数),如果是则输出Yes和最小的另一个SexyPrime;否则输出No并找出比该数大的最小的SexyPrime。

思路

按照题意直接做即可。

解法

#include<bits/stdc++.h>
using namespace std;int n;bool IsPrime(int num) {if (num <= 1) return false;for (int i = 2; i * i <= num; i++) {if (num % i == 0)return false;}return true;
}int IsSexyP(int num) {if (IsPrime(num)) {if (IsPrime(num - 6))return num - 6;else if (IsPrime(num + 6))return num + 6;elsereturn -1;}return -1;
}int FindPrime(int num) {for (int i = num; ; i++) {if (IsSexyP(i) > 0)return i;}return 2;
}int main() {scanf("%d", &n);int ans = IsSexyP(n);if (ans>0) {printf("Yes\n");printf("%d\n", ans);}else {printf("No\n");printf("%d\n", FindPrime(n));}return 0;
}

注意

1156 Sexy Primes (20 point(s)) PAT 素数相关推荐

  1. [Java] 1015. Reversible Primes (20)-PAT甲级

    1015. Reversible Primes (20) A reversible prime in any number system is a prime whose "reverse& ...

  2. 【PAT甲级 素数判断 进制转换】1015 Reversible Primes (20 分) Java版 4/4通过

    题目 思路: 为了提高效率,判断素数采用打表的方式 先计算100000以内的所有素数,然后如果要判断一个数是否为素数的话,直接与表中比对 题目中的意思是: 首先,判断一个数N1是否为素数 如果N1是素 ...

  3. pat数素数 20 c语言,PAT乙级C语言1013 数素数

    1013 数素数 (20 分) 令 P​i表示第 i 个素数.现任给两个正整数 M≤N≤10的4次方,请输出 PM到 PN的所有素数. 输入格式: 输入在一行中给出 M 和 N,其间以空格分隔. 输出 ...

  4. CodeForce 237C Primes on Interval(二分+ 素数筛法)

    题目链接:http://codeforces.com/problemset/problem/237/C Primes on Interval time limit per test 1 second ...

  5. 【题目解析】1015 Reversible Primes (20 分)_27行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A reversible prime in any number system is a prime whose "re ...

  6. A1124 Raffle for Weibo Followers(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【map】

    John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...

  7. 2020年9月PAT甲级满分必备刷题技巧

    2020年7月的考试结束了,除了本次的考题更新,短期内不会更新. [7月题目的特点:首次线上考试,没出链表.树相关的模板题,第2到4题背景新颖,大大降低了抄袭历年代码的可能性,可以看作是线上考试的新趋 ...

  8. 自然数 素数 质数_素数列表–最多20,000个素数的图表

    自然数 素数 质数 Here's a list of all 2,667 prime numbers between zero and 20,000. 以下是所有0至20,000之间的2,667个质数 ...

  9. 素数c分解语言程序,PTA|《C语言程序设计实验与习题指导(第3版)》实验4-2-3 验证“哥德巴赫猜想” (20分)...

    题目 数学领域著名的"哥德巴赫猜想"的大致意思是:任何一个大于2的偶数总能表示为两个素数之和.比如:24=5+19,其中5和19都是素数.本实验的任务是设计一个程序,验证20亿以内 ...

  10. 2019春季PAT考试甲级答案

    20190302春季PAT考试甲级答案 7-1 Sexy Primes (20 分) Sexy primes are pairs of primes of the form (p, p+6), so- ...

最新文章

  1. C语言里面%2d 意思
  2. 并发锁之二:ReentrantReadWriteLock读写锁
  3. HTMLCSS设计与构建网站 笔记HTML
  4. 转 C# 串口编程遇到的问题以及解决方法
  5. 金融贷款逾期的模型构建5——数据预处理
  6. yolov3安卓实现_重磅!MobileNet-YOLOv3来了(含三种框架开源代码)
  7. 自制仿360首页支持拼音输入全模糊搜索和自动换肤
  8. linux下sock_raw和sock_stream读取缓存的区别
  9. 初学习C语言的小Tip
  10. apache源码安装必须依赖的库apr----/etc/ld.so.conf 文件介绍
  11. 华为云obs对象存储使用教程
  12. 中国计算机学会CCF推荐国际学术会议和期刊目录
  13. 如何做好一个产品经理-版本1.0
  14. SIM 卡的IMSI号含义
  15. 【机器视觉】Halcon 19安装教程详解
  16. 小猿圈python学习-Selenium爬虫之使用代理ip的方法
  17. Android绘制地图
  18. adb ps shell 查看进程_adb中ps命令的详解
  19. 京东新通路,零售价值增量发展的新范式
  20. C# 数字转换成数组

热门文章

  1. Target host is not specified解决方案
  2. JS,等额本息,等额本金计算器
  3. 20210412SQL实现全称量词和集合查询
  4. Unity3D基础语言,引擎实战入门教程
  5. 风华散尽,葬送何言?伤感日志分享
  6. runtime error错误
  7. python如何筛选excel重复_老板让我从上千个Excel中筛选数据,利用Python分分钟解决!...
  8. 不容错过的 能源logo设计灵感 标志设计
  9. mysql 联合主键 自增_mysal 数据库联合主键自增问题
  10. vba筛选出满足条件的数据_VBA专题02:使用代码进行复制操作