网易笔试题 路灯

来源:互联网 发布:淘宝店铺开店教程视频 编辑:程序博客网 时间:2024/06/09 19:52

题目描述

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

输入描述:
每组数据第一行两个整数n和l(n大于0小于等于1000,l小于等于1000000000大于0)。第二行有n个整数(均大于等于0小于等于l),为每盏灯的坐标,多个路灯可以在同一点。

输出描述:
输出答案,保留两位小数。

输入例子:
7 15
15 5 3 7 9 14 0

输出例子:
2.5

/** * 路灯坐标从小到大排序,求两个路灯之间距离最大 * 考虑两端情况 */#include <stdio.h>#include <stdlib.h>int compare(const void *a, const void *b) {    return *(int *) a - *(int *) b;}int main() {    int arr[1002];    int n, len;    while (scanf("%d %d", &n, &len) != EOF) {        for (int i = 0; i < n; ++i) {            scanf("%d", &arr[i]);        }        qsort(arr, n, sizeof(arr[0]), compare);        int maxdist, temp1, temp2;        if ((temp1 = (len - arr[n - 1]) * 2) > (temp2 = arr[0] * 2)) {            maxdist = temp1;        } else {            maxdist = temp2;        }        for (int i = 1; i < n; ++i) {            temp1 = arr[i] - arr[i-1];            maxdist = maxdist > temp1 ? maxdist : temp1;        }        printf("%.2lf\n",1.0 * maxdist / 2);    }    return 0;}
0 0
原创粉丝点击