观察如下判断素数的函数:
int is_prime(int n)
{
assert(n>=0); //不满足时终止程序
int m;
int flag=1;
if(n==1) return 0; //1既不是素数也不是合数
m = floor(sqrt(n) + 0.5); //注意*****
for(int i=2;i<=m;i++)
if( n%i == 0) { flag=0; break;}
return flag;
}
上面sqrt()函数的使用,在vc下是能正常运行,但提交到hdoj或者uva时会有(编译错误)Compilation error,如下:
0_0_4909802_31008.cpp 0_0_4909802_31008.cpp(8) : error C2668: “sqrt” : 对重载函数的调用不明确 \include\math.h(626): 可能是“long double sqrt(long double)” \include\math.h(578): 或 “float sqrt(float)” \include\math.h(200): 或 “double sqrt(double)” 试图匹配参数列表“(int)”时 |
原因相信很容易看出来了。
修改为:
m = floor(sqrt((double)n) + 0.5); //顺利ac了
参见 hdoj 2012