Nanyang ACM24-prime number distance problem, Nanyang acm24-prime number

Source: Internet
Author: User

Nanyang ACM24-prime number distance problem, Nanyang acm24-prime number
/*
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 left or right
If there is an equal-distance length prime number, 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
3
6
8
10
Sample output
5 1
7 1
11 1
*/
// Well, written in C language,

# Include <stdio. h>
# Include <math. h>


Int isPrime (int n );


Int main ()
{
Int I, n, num, temp1 = 0, temp2 =-1, flag = 0;
/* While (1)
{
Scanf ("% d", & n );


If (isPrime (n ))
{
Printf ("is Prime \ n", n );
}
Else
{
Printf ("no \ n ");
}

}
*/

Scanf ("% d", & n );
While (n --)
{
Scanf ("% d", & num );
If (num = 1)
{
Printf ("2 1 \ n ");
Continue;
}


For (I = 0; I <num; ++ I)
{
Flag ++;
Temp1 = num + I * temp2;
// Printf ("temp = % d \ n", temp1, temp2 );
Temp2 = temp2 * (-1 );
If (temp1> 2 & temp1 % 10% 2 = 0) | (temp1> 3 & temp1 % 3 = 0) | (temp1> 5 & temp1 % 5 = 0 ))
{
If (flag % 2! = 0)
{
-- I;
}
Continue;
}
Else
{
If (isPrime (temp1 ))
{
Break;
}
}


If (flag % 2! = 0)
{
-- I;
}
}

If (temp1> num)
{
If (isPrime (num-(temp1-num )))
{
Printf ("% d \ n", 2 * num-temp1, temp1-num );
}
Else
{
Printf ("% d \ n", temp1, temp1-num );
}
}
Else if (temp1 <num)
{
Printf ("% d \ n", temp1, num-temp1 );


}
Else if (temp1 = num)
{
Printf ("% d 0 \ n", num );
}
}


Return 0;
}


Int isPrime (int n)
{
Int I = 2,;


A = sqrt (n) + 1;
// Printf ("a = % d % lf \ n", a, sqrt (3.0 ));
If (n = 2)
{
Return 1;
}


For (I = 2; I <= a; ++ I)
{
If (n % I = 0)
{

Return 0;
}
}

Return 1;

}


By http://blog.csdn.net/lp310018931


Nanyang sci-tech oj prime number distance problem, my timeout

A small number of prime numbers that can be modified elsewhere can be used to screen the table with prime numbers.

If you do not know the prime number screening, search for it yourself.


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.