c++难题,关于sqrt函数,请高手赐教

有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程。
例如:
x + y = 9,x * y = 15 ? 找不到这样的整数x和y
1+4=5,1*4=4,所以,加起来等于5,乘起来等于4的二个整数为1和4
7+(-8)=-1,7*(-8)=-56,所以,加起来等于-1,乘起来等于-56的二个整数为7和-8

Input 输入数据为成对出现的整数n,m(-10000<n,m<10000),它们分别表示整数的和与积,如果两者都为0,则输入结束。

Output 只需要对于每个n和m,输出“Yes”或者“No”,明确有还是没有这种整数就行了。

Sample Input 9 15
5 4
1 -56
0 0

Sample Output No
Yes
Yes

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
int m,n,i, t;double x;
while(cin>>m>>n)
{
if(m==0&&n==0)
break;
i=m*m-4*n;
if(i<0)
{cout<<"No"<<endl;continue;}
x=(sqrt(i));t=int(x);
if(t*t!=i)
{cout<<"No"<<endl;continue;}
if((m+t)%2==0&&(m-t)%2==0)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
算法应该没问题,提交到网上后OJ答复:0_0_1637088_663.cpp
0_0_1637088_663.cpp(15) : 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)”时

他的math.h没有sqrt(int)
所以你应该写
sqrt((double)i);
进行强制类型转换
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-08-22
强制类型转化一下试试:
sqrt((double)i)
第2个回答  2009-08-22
我这里显示正常的
第3个回答  2009-08-23
x=(sqrt(i)); // 这一句出问题

x=(sqrt( 1.0 * i)); // 建议改成这样

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网