0

I'm solving this problem:

Vanya walks late t night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns.

Vanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?

In some test it gives WA expected: ** *'22258199.50000*00', found: '22258200.0000000'

but in some tests where answer is not integer, my code output correct answer. (i.e. it can output double)

when I saw the solution there was "printf("%.10f", maxdist/2.)" instead "cout << (double)maxdist/2"

questions:

  • why doesn't my code work?
  • What means the dot at the end of "maxdist/2."?
  • Why there is no "&" before variable maxdist?
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
int main(){
    int n, l;
    cin >> n >> l;
    vector<int> v;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        v.pb(x);
    }
    sort(all(v));
    int maxdist = 2*max(v[0], l-v[n-1]);
    for(int i = 0; i < n-1; i++){
        maxdist = max(maxdist, v[i+1] - v[i]);
    }
    cout << (double)maxdist/2;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 3
    It's a floating point literal. https://en.cppreference.com/w/cpp/language/floating_literal More specifically, in documentation, it's (2) without the optional exponent and suffix. – Blaze Jun 26 '19 at 12:08
  • 3
    Possible duplicate of [What is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) – Yksisarvinen Jun 26 '19 at 12:14
  • What `&` before `maxdist` are you talking about? Why and where do you expect the `&` to be?? – Jabberwocky Jun 26 '19 at 12:18
  • Is this one of those "online" judge tests? If so, some advice on the type of questions -- if the question **requires** you to give floating point results -- **don't take the test and waste your time**. The reason is that floating point is not exact -- the answer you give may not match perfectly with the answer the "judge" is looking for. Your particular problem probably doesn't require any floating point calculations anyway. – PaulMcKenzie Jun 26 '19 at 12:49
  • 1
    Off topic but... please also see ["Why should I not #include ? "](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and ["Why is “using namespace std;” considered bad practice? "](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – G.M. Jun 26 '19 at 13:03

1 Answers1

0

"%.10f" - means floating value of the variable upto 10 decimal places and "." means the decimal point after which any number followed by f will decide how many decimal places the answer would go.