您的位置:首页 > 其它

hdu 2001 计算两点间的距离 sqrt的应用,浮点数 的精度运用

2017-02-07 13:06 302 查看
计算两点间的距离

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 202162 Accepted Submission(s): 70437

Problem Description

输入两点坐标(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

本题思路:

假设l为两点之间的长度

l=sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2))

sqrt是求算数平方根的函数,头文件为 #include< math.h >

例如:8==sqrt(64)

#include <stdio.h>
#include <stdlib.h>
#include<math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
double a,b,c,d;
double l;
while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF)
{
l=sqrt((d-b)*(d-b)+(c-a)*(c-a));
printf("%.2lf\n",l);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: