Problem Description
输入平面坐标系中2点的坐标,输出它们之间的距离
Input
输入4个浮点数x1 y1 x2 y2,分别是点(x1,y1) (x2,y2)的坐标(多组数据)
Output
输出它们之间的距离,保留2位小数(每组数据一行)
Sample Input
1 0 2 0
Sample Output
1.00
1 #include<stdio.h> 2 3 #include<math.h> 4 5 6 7 int main() 8 9 { 10 11 float x1,y1,x2,y2; 12 13 double len; 14 15 while(scanf("%f%f%f%f",&x1,&y1,&x2,&y2)!=EOF) 16 17 { 18 19 len=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); 20 21 printf("%.2lf\n",len); 22 23 } 24 25 return 0; 26 27 }
其他代码:
1 #include<stdio.h> 2 #include<math.h> 3 int main() 4 { 5 double x1,x2,y1,y2; 6 double distance; 7 while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF) 8 { 9 distance=sqrt(fabs(x1-x2)*fabs(x1-x2)+fabs(y1-y2)*fabs(y1-y2)); 10 printf("%.2lf\n",distance); 11 } 12 return 0; 13 }
时间: 2024-09-29 19:42:41