Poj,2689/Runtime error/求大神解答/新手

图雀社区 2016-03-09 08:47:16
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 1000;
long long L,U;
bool vis[maxn];

vector<bool> isPrime(maxn,true);
vector<int> primes(maxn);
void prime(){
memset(vis,true,sizeof(vis));
int pos=1;
primes[0]=2,vis[2]=false;
for(int i=3;i<maxn;i+=2){
if(isPrime[i]){
for(int j=i*i;j<maxn;j+=i)
{
isPrime[j]=false;
}
primes[pos++]=i;
vis[i]=false;
}
}
}

void prime2(){
int ans=0;
int ant=0;
int num1[maxn],k=0,flag=0,m,n,q,p;
memset(num1,0,sizeof(num1));
while(L<=U){
if(!vis[L])
num1[k++]=L;
L++;
}
if(k<=1)
{
printf("There are no adjacent primes.\n");
}
else{
ans=num1[1]-num1[0];
ant=num1[1]-num1[0];
m=num1[0];n=num1[1];p=num1[0];q=num1[1];
for(int i=2;i<k;i++){
int temple=num1[i]-num1[i-1];
if(temple>ant)
{
ant=temple;
m=num1[i-1];
n=num1[i];
}
if(temple<ans)
{
ans=temple;
p=num1[i-1];
q=num1[i];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",p,q,m,n);
}
}
int main(){
prime();
while(~scanf("%lld %lld",&L,&U))
{
prime2();
}
return 0;
}
...全文
160 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
renwotao2009 2016-03-13
  • 打赏
  • 举报
回复
建议一步一步调试

18,772

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧