计算两点间的距离(2001)

来源:互联网 发布:打印机数据线速度 编辑:程序博客网 时间:2024/05/01 17:04

计算两点间的距离

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K(Java/Others)
Total Submission(s): 38109 Accepted Submission(s):14475


Problem Description
输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。


 

Input
输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。


 

Output
对于每组输入数据,输出一行,结果保留两位小数。


 

Sample Input
0 0 0 1 0 11 0


 

Sample Output
1.001.41
C代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    floatx1,y1,x2,y2,d,s;
   while(~scanf("%f %f %f%f",&x1,&y1,&x2,&y2)){
         s=(y2-y1)*(y2-y1)+(x2-x1)*(x2-x1);
         d=sqrt(s);
         printf("%.2f\n",d);
    }
    return0;
}
或者
#include<stdio.h>
#include<math.h>
struct point
{
float x;
float y;
};
int main()
{
double dis,i,j;;
point a,b;
while(scanf("%f %f %f%f",&a.x,&a.y,&b.x,&b.y)!=EOF)
{
i=pow(a.x-b.x,2);
j=pow(a.y-b.y,2);
dis=sqrt(i+j);
printf("%0.2f\n",dis);
}
return 0;
}
注: pow()函数是在C++环境下的函数。其次,就是他的作用了——它是用于计算某个输的N次方的函数!~比如doublepow(double a,double b)函数返回以a为底的b次幂,如果base为零或负和exp小于等于零或非整数时,产生域错误。如果溢出,产生范围错误。 其主要语法为:
#include<math.h>
double pow(double a, double b);
0 0
原创粉丝点击