题目链接:蓝桥杯等差素数列
说明 : 这个题乍一看挺吓人的,其实就是简单的暴力枚举。
题目说什么我们做什么,要找等差素数列,那就一个一个去试就行。
可以先确定一个差不多的范围,用筛选把素数打表,之后暴力去看每一个素数作为首项,能不能构成题上要求的数列。
筛选看这里:https://blog.csdn.net/qq_59700927/article/details/122719762
Code:
#include<bits/stdc++.h>
using namespace std;
const int n = 3000;
bool a[n+1];
//埃氏筛法
void ac()
{
memset(a,true,sizeof(a));
a[0] = a[1] = false;
for (int i = 2; i <= n; i++){
if (a[i]){
for (int j = i * i; j <= n; j += i)
a[j] = false;
}
}
}
int main(void)
{
ac();
for(int i = 2; ; i++)
for(int pos = 2; pos <= 300; pos++){
int cnt = 0;
//判断以i为第一个数,pos为公差的等差素数列是否存在
while(a[i + cnt * pos]){
cnt++;
//能构成的话输出公差pos,终止程序
if(cnt == 10){ cout <<pos; return 0; }
}
}
}