A Prime problem 素数

编程入门 行业动态 更新时间:2024-10-09 09:12:30

A Prime problem  <a href=https://www.elefans.com/category/jswz/34/1764940.html style=素数"/>

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;
}

 

更多推荐

A Prime problem 素数

本文发布于:2024-03-09 01:43:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1723277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:素数   Prime   problem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!