描述
题目标题:
在给定的区间范围内找出所有素数能构成的最大的等差数列(即等差数列包含的素数个数最多)。
详细描述:
接口说明
原型:
int GetMaxArray(unsignedint m, unsignedint n, unsignedint *pArrayNum , unsignedint *pArray)
输入参数:
unsigned int m 给定的区间下限
unsigned int n 给定的区间上限
输出参数:
unsigned int *pArrayNum 等差数列中数字的个数
unsigned int *pArray 等差数列
返回值:
int 0:处理成功 -1:出现异常
限制:
0 <= m
m
#include<iostream>
using namespace std;
int prime[200];
int Count = 0;
void getPrime(int m, int n)
{
int flag = false;
for (int i = m; i <= n; i++)
{
if (i == 1)
{
continue;
}
for (int j = 2; j*j <= i; j++)
{
if (i%j == 0)
{
flag = true;
break;
}
}
if (!flag)
{
prime[Count] = i;
Count++;
}
else
{
flag = false;
}
}
}
int main()
{
int start, end, temp, val = 0, sum = 1, max = 0;
int result[100];
int out[100];
cin >> start >> end;
if (start<0 || end<0 || start>end)
{
return 0;
}
getPrime(start, end);
if (Count>1)
{
temp = prime[1];
}
else if (Count == 1)
{
cout << prime[0] << endl;
}
else
{
return 0;
}
temp = prime[Count - 1] - prime[0];
for (int k = 1; k <= temp; k++)
{
for (int i = 0; i < Count - 1; i++)
{
val = i;
result[0] = prime[i];
for (int j = i + 1; j < Count; j++)
{
if ((prime[j] - prime[val]) == k)
{
val = j;
result[sum] = prime[j];
sum++;
}
}
if (max<sum)
{
max = sum;
for (int i = 0; i < sum; i++)
{
out[i] = result[i];
}
}
sum = 1;
}
}
for (int i = 0; i < max; i++)
{
cout << out[i];
if (i != max - 1)
{
cout << " ";
}
}
return 0;
}