#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes(int num)
{
bool pno[num + 1];
memset(pno, true, sizeof(pno));
for (int i = 2; i* i < = num; i++)
{
if (pno[i] == true)
{
for (int j = i * 2; j < = num; j + = i)
pno[j] = false;
}
}
for (int i = 2; i < = num; i++)
if (pno[i])
cout << i << " ";
}
int main()
{
int num = 15;
cout << "The prime numbers smaller or equal to " << num << " are: ";
SieveOfEratosthenes(num);
return 0;
}
错误在 for 循环中。在我看来,代码是正确的。
尝试在=
.
for(int i=2; i*i<= num; i++) { ..
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句