专栏文章
405J1R第七次训练(T635911 最长非质数子段)
个人记录参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @miotji1j
- 此快照首次捕获于
- 2025/12/03 00:54 3 个月前
- 此快照最后确认于
- 2025/12/03 00:54 3 个月前
错误思路
看不懂题。
正确思路
先来个const int N=5e6+5;再来个bool a[N],然后来个int n,ans,t;在输入n,再来个a[1]=1;然后来个for循环:
CPPfor(int i=2;i<=sqrt(n);i++){
if(a[i]==0){
for(int j=2;j*i<=n;j++){
a[i*j]=1;
}
}
}
再来个for加判断:
CPPfor(int i=2;i<=n;i++){
if(a[i]==1){
t++;
}else{
t=0;
}
ans=max(ans,t);
}
最后输出ans。
正确代码
CPP#include<bits/stdc++.h>
using namespace std;
const int N=5e6+5;
bool a[N];
int n;
int ans,t;
int main(){
cin>>n;
a[1]=1;
for(int i=2;i<=sqrt(n);i++){
if(a[i]==0){
for(int j=2;j*i<=n;j++){
a[i*j]=1;
}
}
}
for(int i=2;i<=n;i++){
if(a[i]==1){
t++;
}else{
t=0;
}
ans=max(ans,t);
}
cout<<ans;
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...