Prime Number distance problem, prime number distance
/* The description now gives you some numbers. You need to write a program, output the adjacent prime numbers of these integers, and output their respective lengths. 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 100*00 * // * 365 187 11011 1 */# include <stdio. h> # include "stdlib. h "// determine whether it is a prime number int isPrime (int num) {if (num = 1) {return 0 ;}for (int I = 2; I * I <= num; I ++) {if (num % I = 0) {return 0 ;}} return 1 ;}int main (int argc, const char * argv []) {// insert code here... int n, num, numUp, numDown, deltUp, deltDown; scanf ("% d", & n); while (n --) {scanf ("% d ", & num); if (isPrime (num) {printf ("% d 0 \ N ", num);} else {numUp = numDown = num; while (! IsPrime (numUp) {numUp + = 1;} while (! IsPrime (numDown) & numDown> 0) {numDown-= 1;} deltUp = numUp-num; deltDown = num-numDown; if (numDown = 0) {printf ("% d \ n", numUp, deltUp);} else if (deltUp> = deltDown) {printf ("% d \ n", numDown, deltDown);} else {printf ("% d \ n", numUp, deltUp) ;}} return 0 ;}