1. 程式人生 > >(hdu 簡單題 128道)hdu 2001 計算兩點間的距離

(hdu 簡單題 128道)hdu 2001 計算兩點間的距離

題目:

計算兩點間的距離

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


Problem Description輸入兩點座標(X1,Y1),(X2,Y2),計算並輸出兩點間的距離。
Input輸入資料有多組,每組佔一行,由4個實陣列成,分別表示x1,y1,x2,y2,資料之間用空格隔開。
Output對於每組輸入資料,輸出一行,結果保留兩位小數。
Sample Input0 0 0 1 0 1 1 0
Sample Output1.00 1.41
Authorlcy
Source
RecommendJGShining   |   We have carefully selected several similar problems for you:  
2002
 2000 2010 2009 2012 

題目分析:

                 簡單題。

程式碼如下:

/*
 * g.cpp
 *
 *  Created on: 2015年3月20日
 *      Author: Administrator
 
 hdu 2001
 */


#include <iostream>
#include <cstdio>
#include <cmath>

struct PPoint{//結構體儘量不要定義成Point這種,容易和C/C++本身中的變數同名
	double x;
	double y;

	PPoint(double _x = 0,double _y = 0):x(_x),y(_y){

	}

	PPoint operator - (const PPoint& op2) const{
		return PPoint(x - op2.x,y - op2.y);
	}

	double operator^(const PPoint &op2)const{
		return x*op2.y - y*op2.x;
	}
};


inline double sqr(const double &x){
	return  x*x;
}

inline double dis2(const PPoint &p0,const PPoint &p1){
	return sqr(p0.x - p1.x) + sqr(p0.y - p1.y);
}

inline double dis(const PPoint& p0,const PPoint& p1){
	return sqrt(dis2(p0,p1));
}


int main(){
	PPoint p1,p2;
	while(scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y)!=EOF){
		printf("%.2lf\n",dis(p1,p2));
	}

	return 0;
}