Nyoj 24 prime number distance problem

Source: Internet
Author: User
Prime Number distance problem time limit: 3000 MS | memory limit: 65535 kb difficulty: 2
Description
Now you are given some numbers. You need to write a program to output the adjacent prime numbers of these integers and their length. If there is a prime number with an equal-distance length between the left and right, the value on the left and the corresponding distance are output.
If the input integer itself is a prime number, the output is the prime number and the distance is 0.
Input
The first line shows the number of test data groups N (0 <n <= 10000)
Each row in the next n Rows has an integer m (0 <m <1000000 ),
Output
Each row outputs two integers a B.
A Indicates the prime number closest to the test data, and B indicates the distance between them.
Sample Input
36810
Sample output
5 17 111 1



Java code:

/** Search for the smallest prime number distance, priority analysis: * 1. Determine whether the input is a prime number, is a prime number output ** 2. Judge the nearest prime number on the left * 3. Judge the nearest prime number on the right ** Special Case, note that number 1 is not a prime number **/package Org. OJ; import Java. util. strong; public class main {// determine the prime number private Boolean is_prime (int x) {If (x> 1) {If (x <4) {// returns true for the prime number 2 and 3 ;} else {for (INT I = 2; I <= math. SQRT (x); I + = 1) {If (X % I = 0) return false;} return true ;}} return false ;} public static void main (string [] ARGs) {custom scan = new custom (system. in); main Nyo J = new main (); int n = scan. nextint (); // number of groups for (INT I = 0; I <n; I ++) {int M = scan. nextint (); If (M <0) break; If (nyoj. is_prime (M) {system. out. println (m + "" + 0); continue;} // distance int d = (M % 2 = 0 )? 1:0; // special case: 1 is not a prime number if (M = 1) {system. out. println ("2 1");} else {While (true) {int min = m-D; int max = m + D; If (nyoj. is_prime (min) {system. out. println (min + "" + d); break;} else {If (nyoj. is_prime (max) {system. out. println (MAX + "" + d); break ;}} D + = 2 ;}} scan. close ();}}


Nyoj 24 prime number distance problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.