当前位置:网站首页>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 !
边栏推荐
- There is no solid line connection between many devices in Proteus circuit simulation design diagram. How are they realized?
- SQL Server 2019 installation error. How to solve it
- 1. Foundation of MySQL database (1- installation and basic operation)
- Detailed explanation of addressing mode in 8086
- Detailed explanation of 14 registers in 8086CPU
- Design an open source continuous deployment pipeline based on requirements
- 新知识:Monkey 改进版之 App Crawler
- Day 4 of pyhon
- Test left shift real introduction
- ROS dynamic parameter configuration: use of dynparam command line tool (example + code)
猜你喜欢

The function of C language string Terminator

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

Freshmen are worried about whether to get a low salary of more than 10000 yuan from Huawei or a high salary of more than 20000 yuan from the Internet

9 Sequence container

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

Gradient epic memory for continuous learning

Use of gt911 capacitive touch screen

MySQL索引(一篇文章轻松搞定)

Exploring shared representations for personalized federated learning paper notes + code interpretation

私有协议的解密游戏:从秘文到明文
随机推荐
[image denoising] image denoising based on partial differential equation (PDE) with matlab code
Missing getting in online continuous learning with neuron calibration thesis analysis + code reading
Vscode outline preview cannot find file symbol
node:打不开/node:已拒绝访问
[college entrance examination] prospective college students look at it, choose the direction and future, and grasp it by themselves
Keil installation of C language development tool for 51 single chip microcomputer
Beginners can't tell the difference between framework and class library
Interview intelligence questions
Elegantly spliced XML
Network packet loss troubleshooting
Source code learning - [FreeRTOS] privileged_ Understanding of function meaning
Federated meta learning with fast convergence and effective communication
2022R2移动式压力容器充装试题模拟考试平台操作
Difference and application of SPI, UART and I2C communication
RT thread studio learning (I) new project
Decoupling in D
libprint2
[image denoising] image denoising based on nonlocal Euclidean median (nlem) with matlab code
鸿蒙os-第一次培训
1.3-1.9 summary