計算兩點間的距離。
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.Scanner;
public class P2001_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
double x1 = sc.nextDouble();
double y1 = sc.nextDouble();
Point p1 = new Point(x1, y1);
double x2 = sc.nextDouble();
double y2 = sc.nextDouble();
Point p2 = new Point(x2, y2);
double res=p1.distance(p2);
System.out.printf("%.2f\r\n", res);
}
}
}
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 res = 0;
res = Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
return res;
}
}
2001、計算球體積
Problem Description
根據輸入的半徑值,計算球的體積。
Input
輸入資料有多組,每組占一行,每行包括一個實數,表示球的半徑。
Output
輸出對應的球的體積,對于每組輸入資料,輸出一行,計算結果保留三位小數。
Sample Input
1
1.5
Sample Output
4.189
14.137
import java.util.Scanner;
public class P2002_2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
double r=sc.nextDouble();
Vulem p=new Vulem(r);
System.out.printf("%.3f\r\n",p.Vulm());
}
}
}
class Vulem{
private double r;
private static double PI=3.14159;
public Vulem(double r){
this.r=r;
}
public double Vulm(){
double vm=PI*4*r*r*r/3;
return vm;
}
}