路灯

1.排序数组;

2.计算数组元素间最大的间隔;

3.考虑首尾的特殊情况;

4.精度问题;

5.数组越界问题。


#include <iostream>
#include <algorithm> 
#include<iomanip>
using namespace std;

int main()
{
  long n,l;
  double d=0;
  cin >> n >> l;
  int ai[n+1];
  for(int i=0;i<n;i++)
  {
    cin >> ai[i];
  }
 
  sort(ai,ai+n);
  
  for(int j=1;j<n;j++)
  {
    if(ai[j]-ai[j-1]>d)
      d=ai[j]-ai[j-1];
  }
  d= d/2.0;
  if(ai[0]!=0&&ai[0]>d)d=ai[0];
  if(ai[n-1]!=l&&l-ai[n-1]>d)d=l-ai[n-1];
  cout<<  setiosflags(ios::fixed) << setprecision(2) <<d<<endl;
  return 0;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
猜你喜欢
  • 2021-12-22
  • 2018-07-24
  • 2021-11-20
相关资源
相似解决方案