HDU2001计算两点间的距离(C,Java用DecimalFormat格式控制)

计算两点间的距离

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


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
 

Source
C语言程序设计练习(一)
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   2023  1002  2054  2035  2037 

C语言AC代码:
#include <stdio.h>
#include <math.h>
int main()
{
    double x1,y1,x2,y2,d;
    while (scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2)!=EOF)
    {
        d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        printf("%.2lf\n",d);
    }
    return 0;
}

Java AC代码:
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			double x1 = sc.nextDouble();
			double y1 = sc.nextDouble();
			double x2 = sc.nextDouble();
			double y2 = sc.nextDouble();
			
			double a =Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
			DecimalFormat df = new DecimalFormat("#0.00");
			
			System.out.println(df.format(a));
		}
	}

}


你可能感兴趣的:(HDU2001,HDU2001Java)