Question 24: Prime Number distance

Source: Internet
Author: User

The description now gives you 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 number of test data groups is N (0 <N <= 10000) from the first line of the output 0) each row of the next N Rows has an integer M (0 <M <1000000), and two integers a B are output for each row. 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 source classic question [cpp]/*********************** * *********** Date: 2013-3-26 * Author: SJF0115 * question: Question 24: Prime Number distance problem * Source: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 24 * result: AC * Source: Nanyang sci-tech OJ * Summary: * *********************************/# include <stdio. h> # include <string. h> # include <math. h> # define MAX 1000010 int prime [MAX]; // Prime number table void prime () {memset (prime, 0, sizeof (prime); for (int I = 2; I <sqrt (MAX) + 1; I ++) {if (prime [I]) {continue;} else {for (int j = I * I; j <MAX; j + = I) {prime [j] = 1 ;}}} int main () {int N, M, LIndex, RIndex; // freopen ("C: \ Users \ SJF \ Users t Op \ acm.txt "," r ", stdin); Prime (); prime [1] = 1; prime [0] = 1; scanf (" % d ", & N); // N groups of test data while (N --) {scanf ("% d", & M); if (prime [M] = 0) {printf ("% d 0 \ n", M) ;}else {RIndex = M; LIndex = M; // find the prime number on the left while (prime [LIndex] = 1 & LIndex> = 0) {LIndex --;} // find the right prime number while (prime [RIndex] = 1) {RIndex ++;} // if (LIndex <0) not found on the left) {printf ("% d \ n", RIndex, RIndex-M);} // else if (M-LIndex <= RIndex-M) {Printf ("% d \ n", LIndex, M-LIndex);} else {printf ("% d \ n", RIndex, RIndex-M) ;}} return 0 ;} /********************************** Date: 2013-3-26 * Author: SJF0115 * question: Question 24: Prime Number distance problem * Source: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 24 * result: AC * Source: Nanyang sci-tech OJ * Summary: * *********************************/# include <stdio. h> # include <string. h> # include <math. h> # define MAX limit 10int prime [MAX]; // Prime number table void prime () {memset (prime, 0, sizeof (prime); for (int I = 2; I <sqrt (MAX) + 1; I ++) {if (prime [I]) {continue;} else {for (int j = I * I; j <MAX; j + = I) {prime [j] = 1 ;}}} int main () {int N, M, LIndex, RIndex; // freopen ("C: \ Users \ SJF \ Desktop \ acm.txt "," r ", stdin); Prime (); prime [1] = 1; prime [0] = 1; scanf ("% d", & N); // N groups of test data while (N --) {scanf ("% d", & M ); if (prime [M] = 0) {printf ("% d 0 \ n", M);} else {RIndex = M; LIndex = M; // find the prime number on the left while (prime [LIndex] = 1 & LIndex> = 0) {LIndex --;} // find the right prime number while (prime [RIndex] = 1) {RIndex ++;} // if (LIndex <0) not found on the left) {printf ("% d \ n", RIndex, RIndex-M);} // else if (M-LIndex <= RIndex-M) {printf ("% d \ n", LIndex, M-LIndex);} else {printf ("% d \ n", RIndex, RIndex-M) ;}} return 0 ;}

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.