注册 登录
编程论坛 C++教室

求100以内的 质数 请高手错误出在哪啊

honghong88 发布于 2011-11-05 22:08, 640 次点击
#include <iostream>
using namespace std;
int main()
{

    int a[100],i,j;
cout<<"1"<<endl<<"2"<<endl;
for(i=2;i<100;i++)
{a[i]=i+1;
for(j=2;j<i;j++)
{if(a[i]%j)
break;
}
cout<<a[i]<<endl;
}
}
3 回复
#2
honghong882011-11-05 22:32
#include <iostream>
using namespace std;
int main()
{

    int a[100],i,j;
    bool s(true);
for(i=2;i<100;i++)
{a[i]=i+1;

for(j=2;j<i+1;j++)
{if(!(a[i]%j))
{s=false;
break;}
}
if(s)
cout<<a[i]<<endl;
s=true;
}
}
#3
honghong882011-11-05 22:32
确实错了 很多地方 现在终于出来了
#4
何凡2011-11-06 13:25

我运行了下楼主二楼的程序,发现没有1和2,建议楼主把 cout<<"1"<<endl<<"2"<<endl;加上就完美了
1