当前位置:网站首页>AcWing——4268. Sexy element
AcWing——4268. Sexy element
2022-06-12 07:22:00 【Java baa】
Catalog
4268. Sexy element
One 、 Test link
Two 、 Topic analysis
| difficulty : Simple |
|---|
| when / Empty limit :0.4s / 64MB |
| source :PAT Class a real topic 1156 |
Algorithm tags : enumeration Prime number Trial division |
3、 ... and 、 Title Description
“ Sexy prime ” It is shaped like ( p , p + 6 ) (p,p+6) (p,p+6) Such a pair of prime numbers .
It's called , Because the Latin tube “ 6、 ... and ” It's called “sex”( That is, English “ sexy ”).
Now give an integer , Please judge whether it is a sexy prime .
Input format
Input gives a positive integer on a line NN.
Output format
if N N N It's a sexy prime , Output in one line Yes, And output and on the second line NN Another sexy prime paired ( If such a number is not unique , The one with the smaller output ).
if N N N Not a sexy prime , Output in one line No, Then output greater than... On the second line NN The minimum number of sexy prime .
Data range
1 ≤ N ≤ 1 0 8 1≤N≤10^8 1≤N≤108
sample input 1:
47
sample output 1:
Yes
41
sample input 2:
21
sample output 2:
No
23
Four 、 Code
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;
}
}
Written in the back
Welcome to your attention , During the implementation, some problems encountered in the work will often be sent .
Please feel free to leave a message to discuss , Let me share with you , Know all but answer !
边栏推荐
- Installation and use of eigen under vs2017
- Explain ADC in stm32
- JDE 对象管理工作平台介绍及 From 的使用
- Day 5 of pyhon
- 面试计算机网络-传输层
- openwrt uci c api
- Why must coordinate transformations consist of publishers / subscribers of coordinate transformation information?
- libprint2
- RT thread studio learning (IX) TF Card File System
- esp32 hosted
猜你喜欢

企业微信官方 加解密库 PHP7版本报错 mcrypt_module_open 未定义方法 并且被PHP抛弃 解决方法使用 openssl解决

Lambda function perfect use guide

Pyhon的第六天

FCPX插件:简约线条呼出文字标题介绍动画Call Outs With Photo Placeholders for FCPX

RT thread studio learning (VIII) connecting Alibaba cloud IOT with esp8266

esp32 hosted

"I was laid off by a big factory"

Non IID data and continuous learning processes in federated learning: a long road ahead

Installation and use of eigen under vs2017

鸿蒙os-第一次培训
随机推荐
RT thread studio learning summary
新知识:Monkey 改进版之 App Crawler
我人生中的第一个需求——Excel数据批量上传到数据库
AI狂想|来这场大会,一起盘盘 AI 的新工具!
d不能用非常ctfe指针
Study on display principle of seven segment digital tube
modelarts二
RT thread studio learning (I) new project
面试计算机网络-传输层
knife4j 初次使用
5、 El expression & JSTL tag library
D
LED lighting experiment with simulation software proteus
AcWing——4269校庆
Detailed explanation of multi coordinate transformation in ROS (example + code)
五、EL 表达式& JSTL 标签库
FCPX插件:简约线条呼出文字标题介绍动画Call Outs With Photo Placeholders for FCPX
MySQL索引(一篇文章轻松搞定)
node:打不开/node:已拒绝访问
12.13-12.19 summary