計算兩點間的距離
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 168512 Accepted Submission(s):
59478
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
import java.util.*;
class Mian{
public static void main(String args[])
{Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{ double x1=cin.nextDouble();
double y1=cin.nextDouble();
double x2=cin.nextDouble();
double y2=cin.nextDouble();
Point p1=new Point(x1,y1);
Point p2=new Point(x2,y2);
double dis=p2.distance(p1);
System.out.printf("%.2f",dis);
System.out.println();
}
}
}
class Point{
private double x;
private double y;
public Point(double x,double y)
{ this.x=x;
this.y=y;
}
public double distance(Point p)
{double dis=Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
return dis;
}
}