ACM 2001 計算兩點間的距離

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

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
Author
lcy

注意:題目中沒有說一定是整數,所以用hasNextInt(),nextInt()是無法通過的。

Java代碼:

import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		double X1,Y1,X2,Y2;
		while(cin.hasNextDouble()){
			X1 = cin.nextDouble();
			Y1 = cin.nextDouble();
			X2 = cin.nextDouble();
			Y2 = cin.nextDouble();
			double distance = Math.sqrt(Math.pow((X1-X2),2) + Math.pow((Y1-Y2),2));
			System.out.println(String.format("%.2f", distance));
		}
		cin.close();
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章