首先我们注意到(不一定是整数)使用double类型·
但是发现提交以后还是错误
以下给出两种解决方案 两种解决方案都要明白——
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
long double a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
if ((a * c + b * d) == e) cout << "true" << endl;
else cout << "false" << endl;
}
return 0;
}
这里我们注意到使用long double(8个字节)后代码就正确了 那为什么double不行呢 以及同为浮点数类型的float也是运行错误——
我们来看以下代码
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
long double a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
if (a * c + d * b >= e)
{
if (a * c + d * b - e <= 1e-6)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
else if(a * c + d * b < e)
{
if ( e-a * c + d * b<= 1e-6)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
}
return 0;
}
double 的两个数判断相等不能用 ==
而是差值的绝对值小于很小的数:1e-6;
如同代码中的这两段语句——
if (a * c + d * b - e <= 1e-6)
if ( e-a * c + d * b<= 1e-6)