抽中算我输
题目描述
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
A “prime” number is an integer greater than 1 with only two divisors: 1 and itself; examples include 5, 11 and 23. Given a positive integer, you are to print a message indicating whether the number is a prime or how close it is to a prime.
输入描述:
The first input line contains a positive integer,n (n ≤ 100), indicating the number of values to check. The values are on the following n input lines, one per line. Each value will be an integer between 2 and 10,000 (inclusive).
输出描述:
For each test case, output two integers on a line by itself separated by a space. The first integer is the input value and the second integer is as follows:
- If the input number is a prime, print 0 (zero).
- If the input number is not a prime, print the integer d where d shows how close the number is to a prime number (note that the closest prime number may be smaller or larger than the given number).
示例1
输入
复制
4
23
25
22
10000
输出
复制
23 0
25 2
22 1
10000 7
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
int swap1(int max1,int min1){
return min1<max1?min1:max1;
}
int main(){
int n,x;
scanf("%d",&n);
int i,j,k=1,max1,min1;
int a[10000]={0};
a[0]=2;
for(i=3;i<10008;i+=2){
for(j=2;j<sqrt(i);j++){
if(i%j==0){
break;
}
}
if(j>sqrt(i)){
a[k]=i;
k++;
}
}
while(n--){
max1=0;
min1=0;
scanf("%d",&x);
for(i=0;i<k;i++){
if(a[i]<=x && a[i+1]>=x){
max1=a[i+1]-x;
min1=x-a[i];
}
}
min1=swap1(max1,min1);
cout<<x<<" "<<min1<<endl;
}
return 0;
}