計算兩點間的距離 Time Limit: 2000/1000 MS (java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 202162 Accepted Submission(s): 70437
輸入兩點坐標(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
本題思路: 假設l為兩點之間的長度 l=sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2)) sqrt是求算數平方根的函數,頭文件為 #include< math.h > 例如:8==sqrt(64)
#include <stdio.h>#include <stdlib.h>#include<math.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char *argv[]) { double a,b,c,d; double l; while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF) { l=sqrt((d-b)*(d-b)+(c-a)*(c-a)); printf("%.2lf/n",l); } return 0;}新聞熱點
疑難解答