输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。
Input
输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。
Output
对于每组输入数据,输出一行,结果保留两位小数。
Sample Input
0 0 0 1
0 1 1 0
Sample Output
1.00
1.41
#include<iostream>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
float x1,y1,x2,y2;
double d;
while(cin>>x1>>y1>>x2>>y2){
d=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
cout.setf(ios::fixed);
cout<<setprecision(2)<<d<<endl;
}
return 0;
}
如何保留两位小数
假如是printf,就比较省事:printf("%.2f",d)
,但用printf麻烦的地方在于,你要时刻记得对应数据类型,比如这里就不能用%d而是%f
假如用cout呢?
- 添加头文件#include
- 假如是保留整个浮点数的位数(小数点前后都算),直接用
cout<<setprecision(2)<<d