在求200到300的素数的时候,为什么输出了除了素数以外的数字,函数到底除了什么问题?
m函数的break有问题,返回的值会出现错误,建议这里的break变成return -1
在主函数里面判断一下,如果返回值是-1,就continue
望采纳!谢谢
1.将break改为return 0;
2.设置if(y!=0)执行显示程序
参考代码
#include <stdio.h>
#include <math.h>
#include <iostream>//不需要的c++头文件
int m(int x)
{
int y, a = 0, n = x - 1;
for (y = 2; y < x; y++)
{
if (a != x%y)
{
if (y < n)
{
continue;
}
else
{
return x;
}
}
else
{
return 0;
}
}
}
void main()
{
int x, y;
printf("200到300的素数");
for (x = 200; x <= 300; x++)
{
y = m(x);
if (y!=0)
printf(" %d", y);
}
system("pause");//c++调用窗口显示程序,无需理会。
}