sqrt()函数注意

观察如下判断素数的函数:
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

 
 

                
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值