当前位置: 首页 > news >正文

【PTA Advanced】1156 Sexy Primes(C++)

目录

题目

Input Specification:

Output Specification:

Sample Input 1:

Sample Output 1:

Sample Input 2:

Sample Output 2:

思路

代码


题目

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).

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

思路

这题简单到离谱的地步!

唯一需要注意的点是测试点2素数应该是一个正数,所以在判断n-6是否是素数之前要先判断一下n-6是否是正数。

代码

#include <iostream>
#include <cmath>using namespace std;// 判断x是否为素数
bool isPrime(int x) {if(x==1) return false;for(int i=2;i<=sqrt(x);i++)if(x%i==0) return false;return true;
} int main(int argc, char** argv) {int n;cin>>n;if(isPrime(n)) {if(n-6>0&&isPrime(n-6)) {printf("Yes\n%d",n-6);return 0;}else if(isPrime(n+6)) {printf("Yes\n%d",n+6);return 0;}}cout<<"No"<<endl;while(true) {n++;if(isPrime(n)&&(n-6>0&&isPrime(n-6)||isPrime(n+6))) {cout<<n;break;}}return 0;
}

http://www.lryc.cn/news/2466.html

相关文章:

  • 项目(今日指数)
  • 适配器模式(Adapter Pattern)
  • 网易一面:select分页要调优100倍,说说你的思路? (内含Mysql的36军规)
  • 二叉树的遍历 (2023-02-11)
  • string的深浅拷贝问题
  • C++中的万能头文件
  • Java 8 Lambda 表达式 Stream
  • 【VictoriaMetrics】VictoriaMetrics单机版部署(二进制版)
  • SCI论文阅读-使用基于图像的机器学习模型对FTIR光谱进行功能组识别
  • 双11大型互动游戏“喵果总动员” 质量保障方案总结
  • 剑指Offer专项突击版题解一
  • Django框架之模型
  • OSACN-Net:使用深度学习和Gabor心电图信号谱图进行睡眠呼吸暂停分类
  • 使用开源实时监控系统 HertzBeat 5分钟搞定 Mysql 数据库监控告警
  • 插件 sortablejs:HTML元素可拖动排序
  • libVLC 视频裁剪
  • LAMP架构介绍及配置
  • Android图形显示流程简介
  • 4.5.3 ArrayList
  • 十二、Linux文件 - fseek函数讲解
  • Python3.10新特性之match语句示例详解
  • 虎牙盈利能力得到改善,但监管风险对其收入产生负面影响
  • HBase 分布式搭建
  • 【Python】修改枚举的取值及链式调用
  • 复现篇--zi2zi
  • 153、【动态规划】leetcode ——416. 分割等和子集:滚动数组(C++版本)
  • linux head命令(head指令)(获取文件或管道输出结果前n行,默认前10行)与sed命令区别
  • Mysql数据库09——分组聚合函数
  • 第43章 菜单实体及其约束规则的定义实现
  • OpenAI最重要的模型【CLIP】