#include <iostream>
#include <math.h>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int x1,y1,x2,y2;
while(cin>>x1>>y1>>x2>>y2)
{
double d;
d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
cout<<fixed<<setprecision(2)<<d<<endl;
}
system("pause");
return 0;
}
错误原因
d为double类型,而后面的x1,y1,x2,y2均为int类型
改正方式
#include <iostream>
#include <math.h>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
double x1,y1,x2,y2;
while(cin>>x1>>y1>>x2>>y2)
{
double a = x1-x2;
double b = y1-y2;
double d;
d = sqrt(a*a+b*b);
cout<<fixed<<setprecision(2)<<d<<endl;
}
system("pause");
return 0;
}