当前位置:网站首页>性感素数(Acwing每日一题)
性感素数(Acwing每日一题)
2022-07-27 05:21:00 【最后一只三脚兽】

题目较简单,就是判断一个数num是否是性感素数只要三步
- num是否是素数
- num+6是否是素数,是就返回num-6
- num-6是否是素数,是就返回num+6
先开始判断一下,如果不是性感素数就用while循环让num不断+1,直到找到性感素数为止。
#include<bits/stdc++.h>
using namespace std;
int N;
bool judge(int num) {
if (num < 2) return false;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0)return false;
}
return true;
}
//获取匹配的性感素数,没有就返回-1
int getSex(int num) {
if (!judge(num))return -1;
if (judge(num - 6))return num - 6;
if (judge(num + 6))return num + 6;
return -1;
}
int main()
{
cin >> N;
if (getSex(N) != -1) {
cout << "Yes" << endl;
cout << getSex(N) << endl;
return 0;
}
cout << "No" << endl;
while (N++) {
if (judge(N)) {
if (getSex(N) != -1) {
cout << N << endl;
return 0;
}
}
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
IOT operating system
arcgis for js api-入门系列
[song] rebirth of me in py introductory training (7): function call
C语言--字符串操作函数与内存操作函数
Can it replace PS's drawing software?
回调使用lambda
Kaggle调用自定义模块方法
Unity Shader 概述
[first song] rebirth of me in py introductory training (3): if conditional sentence
C语言-程序的编译
QGIS系列(1)-QGIS(server-apache) win10安装
[song] rebirth of me in py introductory training (12): Matplotlib interface and common graphics
【动态规划----钢条切割问题】
【头歌】重生之我在py入门实训中(2):公式编程
开源WebGIS-相关知识
古老的艺术-用好长尾关键词
【Arduino】重生之Arduino 学僧(1)
Stm32-fsmc extended memory SRAM
[first song] rebirth of me in py introductory training (2): formula programming
力扣题解 动态规划(4)








