连续非素数子序列的最大长度
给出一个正整数 n,求在数列 {2,3,⋯,n} 中,连续非素数子序列的最大长度。
#include <bits/stdc++.h>
using namespace std;
int x[5000010];
int main()
{
int n, maxn = 0;
cin >> n;
for (int i = 2; i * i <= n; i++)
if (x[i] == 0)
for (int j = 2; i * j <= n; j++)
x[i * j] = 1;
for (int i = 2; i <= n; )
{
if (x[i] == 1)
{
int cnt = 0;
while (x[i] == 1 )
{
cnt++;
i++;
}
if (cnt > maxn)
maxn = cnt;
}
else
i++;
}
cout << maxn << endl;
return 0;
}