wuzeyong @ 2022-12-04 17:12:32
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
int n,i;
bool a[5001];
bool b=true;
memset(a,true,sizeof(a));
cin>>n;
for(i=1;i<=n;i++)
for(int j=i;j<n;j+=i)
a[j]=not a[j];
for(i=1;i<=n;i++)
{
if(a[i]==0)
{
if(b==0)
cout<<" ";
cout<<i;
b=false;
}
}
return 0;
}
by mysterys @ 2022-12-04 17:43:04
@wuzeyong
for(i=1;i<=n;i++)
for(int j=i;j<n;j+=i)
a[j]=not a[j];
for(i=1;i<=n;i++)
for(int j=i;j<=n;j+=i)
a[j]=not a[j];
就可以了
by mysterys @ 2022-12-04 17:43:35
@wuzeyong 你少写了一个等于号
by mysterys @ 2022-12-04 17:47:38
@wuzeyong 另外您的输出我也帮您优化了:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i;
bool a[5001];
memset(a,true,sizeof(a));
cin>>n;
for(i=1;i<=n;i++)
for(int j=i;j<=n;j+=i)
a[j]=!a[j];
for(i=1;i<=n;i++)
{
if(a[i]==0)
{
cout<<i<<" ";
}
}
return 0;
}
已经AC了:AC
by wuzeyong @ 2022-12-04 18:56:52
@mysterys 非常感谢!!!
by mysterys @ 2022-12-04 20:27:00
@wuzeyong 给个关注谢谢
by SNXL @ 2023-03-24 20:56:11
@mysterys 谢谢