最远距离
Description
给定一组点 (x, y)(第一象限),求距离最远的两个点之间的距离。
Input
第一行是点数 n(n 大于等于 2);接着每一行代表一个点,由两个浮点数x y组成。
Output
输出一行是最远两点之间的距离。
Sample Input 1
6 34.0 23.0 28.1 21.6 14.7 17.1 17.0 27.2 34.7 67.1 29.3 65.1
Sample Output 1
53.8516
Hint
C语言
使用printf("%.4f", dis);精确到小数点后4位,
C++
使用cout << fixed << setprecision(4) << dis << endl; 输出距离值并精确到小数点后 4 位。
fixed 和setprecision 是在 头文件里定义的格式控制操作符,需要#include 。
tips:小数点后不足四位不需补0
Source
none
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n;
scanf("%d",&n);
float pointX[n],pointY[n];
for(int i=0;i<n;i++){
scanf("%f %f",&pointX[i],&pointY[i]);
}
double maxdistance=0,distemp=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
distemp=sqrt((pointX[i]-pointX[j])*(pointX[i]-pointX[j])+(pointY[i]-pointY[j])*(pointY[i]-pointY[j]));
if(maxdistance<distemp){
maxdistance=distemp;
}
}
}
printf("%.4f",maxdistance);
return 0;
}