Блог пользователя sidchelseafan

Автор sidchelseafan, 10 лет назад, По-английски

Your text to link here... Hi, I was doing this problem on SPOJ. It basically asks us to find total number of distinct slopes among n different points. Pretty easy( n <= 200).

So I did it using two approaches. First, I run two loops calculating all the slopes (the points are integers,and I took care and took input as doubles) and insert them in a set < double > taking care of case when dx =0 (slope = dy/dx). This approach got WA.

In second, I do the same thing but I push the slopes in a vector and then sort the vector and take care of duplicates. This Got AC.

Your text to link here...- Approach 1.-WA

Your text to link here...- Approach 2.- AC.

Can someone explain me the reason for this.

Thank You.

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
10 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

One does not simply check equality in doubles by ==, !=. Identical doubles can differ in the last few significant digits, so you should check if their absolute difference is small enough.

  • »
    »
    10 лет назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

    Oh !. But regardless, it worked in the second approach. I don't know why using a set gave WA and vector AC. Could you be more explicit. Thank You.

    • »
      »
      »
      10 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      My guess is luck or that equality comparison with doubles messes up sets somehow.