素数距离问题
来源:互联网 发布:淘宝客户端官方下载 编辑:程序博客网 时间:2024/05/17 04:08
素数距离问题
时间限制:3000 ms | 内存限制:65535 KB
难度:2
- 描述
- 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度。如果左右有等距离长度素数,则输出左侧的值及相应距离。
如果输入的整数本身就是素数,则输出该素数本身,距离输出0- 输入
- 第一行给出测试数据组数N(0<N<=10000)
接下来的N行每行有一个整数M(0<M<1000000), - 输出
- 每行输出两个整数 A B.
其中A表示离相应测试数据最近的素数,B表示其间的距离。 - 样例输入
36810
- 样例输出
5 17 111 1
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=24
AC code:
#include <iostream>#include <cmath>#include <cstdio>using namespace std;bool fun(int n){ int i,flag=true; if(n<2) return false; for(i=2; i<=sqrt(n); i++) { if(n%i==0) { flag=false; break; } } if(flag) return true; return false;}int main() { int t,n,j; cin>>t; while(t--){ j=1; cin>>n; if(fun(n)){ cout<<n<<" "<<0<<endl; } else{ while(!fun(n-j) && !fun(n+j)) j++; if(fun(n-j)) cout<<n-j<<" "<<j<<endl; if(!fun(n-j) && fun(n+j)) cout<<n+j<<" "<<j<<endl; } } return 0;}
0 0
- 素数距离问题
- NYOJ - 素数距离问题
- 素数距离问题
- NYOJ24-素数距离问题
- 素数距离问题
- 素数距离问题
- 素数距离问题
- 【素数距离问题 24】
- 素数距离问题
- 素数距离问题
- 素数距离问题
- 素数距离问题
- nyoj24素数距离问题
- nyoj24 素数距离问题
- NYOJ24 素数距离问题
- 素数距离问题
- 素数距离问题
- 素数距离问题
- 烦 烦 烦
- 搞搞文艺范儿
- leetcode-Add Two Numbers
- 青春——人生的苦茶
- samba配置
- 素数距离问题
- Android Support v4、v7、v13的区别和应用场景
- 【WebApp】webApp开发总结(集合贴)
- java ArrayBlockingQueue 使用示例
- EBS 不能打开MES Supervisor 职责
- 在Javascript删除列表项中的下拉项怎么弄
- js问题.切记...不要犯错...
- Java Timer&TimerTask原理分析
- 并发*程同步