我认为是对的,但是它找不到质数。我不知道它有什么错误。
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int i, j, n, a[101];
for (i = 1; i <= 100; i++)
a[i] = i;
a[1] = 0;
for (i = 2; i < sqrt(100); i++);
for (j = i + 1; j <= 100; j++)
{
if (a[i] != 0 && a[j] != 0)
if (a[j] % a[i] == 0)
a[j] = 0;
}
cout << endl;
for (i = 1; i <= 100; i++)
{
if (a[i] != 0)cout << a[i] << " ";
}
cout << endl;
system("pause");
}
它输出从 2 到 99 的所有数字,而不是素数
您的外for
循环没有主体。
for (i = 2; i < sqrt(100); i++);
去掉分号。curly braces
即使循环体只是一个语句,也始终用于标记循环的边界。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句