输入若干个正整数,将其中的素数输出来。
代码如下:
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int n);
int main( )
{
int n;
while(cin>>n)
{
if(isPrime(n))
cout<<n<<endl;
}
return 0;
}
bool isPrime(int n)
{
int i;
for (i=2; i<=sqrt(n); ++i)
if (n%i==0)
break;
if (i>sqrt(n))
return n;
}
程序运行的时候有警告:control reaches end of non-void function [-Wreturn-type] 求路过的大神解释下与给个解决方案,碰到过好几次这种情况了,度娘给的解释看是看懂了,还是不知道原因和消除警告的方案。
原文:http://blog.csdn.net/liuchang54/article/details/42177899