注册 登录
编程论坛 C++教室

[求助]奇怪的问题,究竟是哪儿不对.

tancui 发布于 2007-09-15 14:16, 381 次点击

//找在10000内的质素里等差最大的等差数列 如3 5 7差为2

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main(int argc, char* argv[])
{
const int SIZE = 10000;
int num[SIZE+1]; //一个栅栏,即num[0];
num[0]=0;
num[1]=0;

for(int m=2;m<=SIZE;m++) //set all the num 1
num[m]=1;

int pp=sqrt(SIZE)+1;
for (int a=2;a!=pp;a++)
for(int b=2;b<=sqrt(a);b++) //find the prime number in sqrt(SIZE) and set others 0
if(a%b==0)
{
num[a]=0;
break;
}

for(int i=1;i!=pp;i++) // find tne prime number in SIZE and set them 1
{
if(num[i]==0)
continue;

for (int j=2;j<=SIZE / i;j++)
num[i*j]=0;
}
vector<int> PN;

for(int n=1;n<SIZE;n++) //save the prime number in vector PN
if(num[n]==1)
PN.push_back(n);

vector<int>::iterator Pb=PN.begin();
vector<int>::reverse_iterator Pe=PN.rbegin();

int aa[4]={1,1,1,1};
vector<int> L(aa,aa+4);

int abc=PN.size()/2;

for (int N=0;N < abc;N++,Pb++)
{

for (int M=0;M < abc;M++,Pe++)

{

L[3]=(*Pb+*Pe)/2;
if(find(PN.begin(),PN.end(),L[3]) != PN.end()) //在PN中查找L[3],
if(L[0]<*Pe-*Pb)
{
L[0]=*Pe-*Pb;
L[1]=*Pe;
L[2]=*Pb;
}
}
}
cout << L[0] <<endl
<<L[1] <<endl
<<L[2] <<endl;
return 0;
}

1 回复
#2
tancui2007-09-15 14:18
L[3]=(*Pb+*Pe)/2;
*Pb-*Pe;

主要是这种的问题,放到循环外面就可以,但在内部时只运行几次就挂了.
1