当前位置:网站首页>AcWing——4268. 性感素
AcWing——4268. 性感素
2022-06-12 07:15:00 【Java咩】
4268. 性感素
一、测试链接
二、题目分析
| 难度:简单 |
|---|
| 时/空限制:0.4s / 64MB |
| 来源:PAT甲级真题1156 |
算法标签:枚举质数试除法 |
三、题目描述
“性感素数 ”是指形如 ( p , p + 6 ) (p,p+6) (p,p+6) 这样的一对素数。
之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。
现给定一个整数,请你判断其是否为一个性感素数。
输入格式
输入在一行中给出一个正整数 NN。
输出格式
若 N N N 是一个性感素数,则在一行中输出 Yes,并在第二行输出与 NN 配对的另一个性感素数(若这样的数不唯一,输出较小的那个)。
若 N N N 不是性感素数,则在一行中输出 No,然后在第二行输出大于 NN 的最小性感素数。
数据范围
1 ≤ N ≤ 1 0 8 1≤N≤10^8 1≤N≤108
输入样例1:
47
输出样例1:
Yes
41
输入样例2:
21
输出样例2:
No
23
四、代码
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
sexyPrime();
}
public static void sexyPrime() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean nIsPrimeNumber = isPrimeNumber(n);
boolean nDiffSix = isPrimeNumber(n - 6);
if (nIsPrimeNumber && nDiffSix) {
System.out.println("Yes");
System.out.println(n - 6);
return;
}
boolean nAddSix = isPrimeNumber(n + 6);
if (nIsPrimeNumber && nAddSix) {
System.out.println("Yes");
System.out.println(n + 6);
return;
}
System.out.println("No");
Set<Integer> set = new HashSet<>();
int num = n - 5;
while (true) {
if (isPrimeNumber(num)) {
set.add(num);
}
if (set.contains(num - 6) && set.contains(num)) {
System.out.println(num - 6 > n ? num - 6 : num);
return;
}
num++;
}
}
public static boolean isPrimeNumber(int num) {
if (num <= 3) {
return num > 1;
}
for (int i = 2; i <= Math.pow(num, 0.5); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
写在后面
欢迎关注,实现期间会经常发一些工作中遇到的问题。
欢迎随时留言讨论,与君共勉,知无不答!
边栏推荐
- Dynamic coordinate transformation in ROS (dynamic parameter adjustment + dynamic coordinate transformation)
- 3 strings, containers, and arrays
- Imx6q pwm3 modify duty cycle
- [data clustering] data set, visualization and precautions are involved in this column
- leetcode. 39 --- combined sum
- [image denoising] image denoising based on partial differential equation (PDE) with matlab code
- NOI openjudge 计算2的N次方
- 8086/8088 instruction execution pipeline disconnection reason
- How to update kubernetes certificates
- Error mcrypt in php7 version of official encryption and decryption library of enterprise wechat_ module_ Open has no method defined and is discarded by PHP. The solution is to use OpenSSL
猜你喜欢

Construction of running water lamp experiment with simulation software proteus

sql——课程实验考查

Detailed explanation of TF2 command line debugging tool in ROS (parsing + code example + execution logic)

Planning and design of 1000 person medium-sized campus / enterprise network based on ENSP and firewall (with all configuration commands)

Troubleshooting of cl210openstack operation -- Chapter experiment

Nine project management issues that PM should understand
![[image detection] SAR image change detection based on depth difference and pcanet with matlab code](/img/c7/05bfa88ef1a4a38394b81755966e46.png)
[image detection] SAR image change detection based on depth difference and pcanet with matlab code

Error mcrypt in php7 version of official encryption and decryption library of enterprise wechat_ module_ Open has no method defined and is discarded by PHP. The solution is to use OpenSSL

I met 15 people recently and found that I couldn't answer the basic question of this test

Kali与编程:如何快速搭建OWASP网站安全实验靶场?
随机推荐
Descscheduler secondary scheduling makes kubernetes load more balanced
Circular linked list and bidirectional linked list - practice after class
Drawing grid navigation by opencv map reading
RT thread studio learning summary
d中的解耦
d不能用非常ctfe指针
Can official account also bring goods?
@DateTimeFormat @JsonFormat 的区别
最近面了15个人,发现这个测试基础题都答不上来...
Category 7
There is no solid line connection between many devices in Proteus circuit simulation design diagram. How are they realized?
Planning and design of 1000 person medium-sized campus / enterprise network based on ENSP and firewall (with all configuration commands)
2 variables and basic types
Postman splice replacement parameter loop call interface
New knowledge: monkey improved app crawler
libprint2
Test manager defines and implements test metrics
Test left shift real introduction
2022电工(初级)考试题库及模拟考试
初中学历,从不到3K,到月薪30K+,不设限的人生有多精彩