.
main( , & ( n -- > , & , get_nearest( x ) );
prime_list * get_nearest( step = ; sign = -; Node * head = NULL ; ( ! be_prime( x , &+= ( sign = - sign ) * ++
be_prime( x , Node * * pp ) ( x <= ( x == ( get_remainder( x , pp ) == )
get_remainder( x , Node * * pp ) ( * pp == NULL || sqr_less( (*pp) -> prime , x ) ) add_1_prime ( pp ) ; * p = * ( p != ( x % p -> prime == = p -> ! sqr_less ( n , n * n <
add_1_prime ( pp ) ;
sqr_less( (*pp) -> prime , x )
add_1_prime( Node * * ( * pp == , pp ); next_p = ( * pp )->prime + ; ( !++}
be_prime( next_p , pp )
be_prime( x , & head )
add_1_prime ( pp ) ;
be_prime( next_p , pp )
Get_nearest (step =; sign =-; Node * head = NULL; (create a prime number table () with the largest square of prime numbers not less than x (),! Be_prime (x, & + = (sign =-sign) * ++
add_1_prime( Node * * ( * pp == , pp ); ( ( * pp ) -> : add ( : add ( next_p = ( * pp )->prime + ; ( !++
Problem:
Prime Number
In an information and communication Museum in the World Expo Park, visitors can use mobile phones and other terminals to participate in interactive games and play Guess games with virtual characters Kr. Kong.
When an integer X is displayed on the screen, you will receive an unexpected gift if you can give the prime answer closest to it faster than Kr. Kong.
If X itself is a prime number, the answer is X. If there are two prime numbers closest to X, the answer is greater than its prime number.
Next there are N rows, each row has a positive integer X
Output: N rows are output, and each row is the prime number closest to X.
4
22
5
18
8
Output
23
5
19
7
Source: http://www.cnblogs.com/pmer/ "C language beginners code in the Common Errors and flaws" series blog
*/
#include <stdio.h><stdbool.h> prime_list * get_nearest( be_prime( , Node * * get_remainder( , Node * * add_1_prime( Node * * sqr_less ( , add ( , Node * * my_malloc( Node * * my_free( Node * main( , & ( n -- > , & , get_nearest( x ) ); get_nearest( step = ; sign = -; Node * head = NULL ; ( ! be_prime( x , &+= ( sign = - sign ) * ++ be_prime( x , Node * * pp ) ( x <= ( x == ( get_remainder( x , pp ) == ) get_remainder( x , Node * * pp ) ( * pp == NULL || sqr_less( (*pp) -> prime , x ) ) add_1_prime ( pp ) ; * p = * ( p != ( x % p -> prime == = p -> ! sqr_less ( n , n * n < add_1_prime( Node * * ( * pp == , pp ); ( ( * pp ) -> : add ( : add ( next_p = ( * pp )->prime + ; ( !++ add ( prime , Node * **&-> prime =-> next = ** pp = my_malloc( Node * * ( ( * p_p = malloc( (* * p_p) ) ) == my_free( Node ** ( ( temp = p ) !== p->
Related blog
I accidentally found that a Jingle Guo user later studied a blog post on the same issue. I felt that the user reading this article may have some reference value, so I would like to provide the relevant link here :.