路灯最短距离

题目:一条长l的笔直的街道上有n个路灯,若这条街的起点为0,终点为l,第i个路灯坐标为ai ,每盏灯可以覆盖到的最远距离为d,为了照明需求,所有灯的灯光必须覆盖整条街,但是为了省电,要是这个d最小,请找到这个最小的d。

import java.util.*;

import java.text.DecimalFormat;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();//灯的个数
int length=sc.nextInt();//路的长度
int[] location=new int[n];//灯的位置
for(int i=0;i<n;i++){
location[i]=sc.nextInt();
}
//关闭输入流
sc.close();
//将路灯的位置排序
Arrays.sort(location);
double max=location[0];//第一个位置到0坐标的距离
double temp=length-location[n-1];//最后一个灯到末尾的距离
if(temp>max){
max=temp;
}
for(int i=1;i<n;i++){
double stemp=(location[i]-location[i-1])/2;
if(stemp>max){
max=stemp;
}
}
        BigDecimal bigDecimal=new BigDecimal(max);
        System.out.println(bigDecimal.setScale(2));
}
}

猜你喜欢

转载自blog.csdn.net/zyilove34/article/details/74316080