A Prime problem 素数

抽中算我输


题目描述


时间限制: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值