计算两点间的距离

计算两点间的距离

这道题本身不难的,主要用于练习C++的cout格式化输出

  • ios::fixed  将符点数按照普通定点格式处理(非科学计数法)
  • 与<iomanip>的setprecision配合使用,可以表示指定小数点后面的保留位数
#include <iostream>
#include <iomanip>
#include<string.h>
#include<math.h>
using namespace std;
double x11,y11,x22,y22;
double calculate(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
    cout.setf(ios::fixed);
    while(cin>>x11>>y11>>x22>>y22){
        double k=calculate(x11,y11,x22,y22);
        cout<<setprecision(2)<<k<<endl;
    }
    return 0;
}