1

I have got a problem to check if three points form a triangle or not. If it forms a triangle the program should print the square of maximum length of the three sides of the triangle. If not the program will print "Coolinear".
Anyways here is a sample of what I have tried:

#include <iostream>
#include <cmath>
using namespace std;
int main () {
  double x1,y1,x2,y2,x3,y3;
  double area;
  double s1,s2,s3;


cin >> x1 >> y1;
cin >> x2 >> y2;
cin >> x3 >> y3;


    area = 0.5*abs(((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)));
    s1 = ((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2));
    s2 = ((x2-x3)*(x2-x3))+((y2-y3)*(y2-y3));
    s3 = ((x1-x3)*(x1-x3))+((y1-y3)*(y1-y3));
if (area!=0){

     if (s1 >= s2 && s1 >= s3)
        cout<<s1<<endl;
     if (s2 >= s1 && s2 >= s3)
        cout<<s2<<endl;
     if (s3 >= s1 && s3 >= s2)
        cout <<s3<<endl;
}

else
    cout <<"Coollinear";
return 0;

} 

I submitted this code on codeforces website as it is my last problem on a contest. It gives me wrong answer at test 9, What else should I use? and Why is my answer wrong?

Anyways Here is the text of the problem:
Like all problem solvers, Meiko loves eating crepe! As we all know, crepe usually served in a triangular shape. Now Meiko wants to know how large can a crepe side be! So he tries to draw a triangle on a plane using three points and calculate the maximum length of the three sides of the triangle. But sometimes he falls asleep as he has been busy with the team preparing the training problems! As a result, the three points he uses may not form a triangle that could represent a piece of crepe! A triangle can represent a piece of crepe only if it has a positive area. So you are here to help Meiko! Given the coordinates Meiko used, determine whether they form a triangle that could represent a piece of crepe or not.

Input Three integer coordinates (X,Y) that represent the three points Meiko used. Each point on a separate line. (-10^9<=X,Y<=10^9)

Output If the points form a triangle that can represent a piece of crepe, print the square of the maximum length of the three sides of the triangle. Otherwise print "Collinear" without quotes.

2 Answers2

0

If at least two sides have same length and are longer than the third one then this code will output multiple results. The three if-statements must be fixed

#include <iostream>
#include <cmath>
using namespace std;
int main () {
  double x1,y1,x2,y2,x3,y3;
  double area;
  double s1,s2,s3;


cin >> x1 >> y1;
cin >> x2 >> y2;
cin >> x3 >> y3;


    area = 0.5*abs(((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)));
    s1 = ((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2));
    s2 = ((x2-x3)*(x2-x3))+((y2-y3)*(y2-y3));
    s3 = ((x1-x3)*(x1-x3))+((y1-y3)*(y1-y3));
if (area!=0){

     if (s1 >= s2 && s1 >= s3)
        cout<<s1<<endl;
     else if (s2 >= s1 && s2 >= s3)
        cout<<s2<<endl;
     else if (s3 >= s1 && s3 >= s2)
        cout <<s3<<endl;
}

else
    cout <<"Coollinear";
return 0;

} 
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
0

Your code will output multiple results if the maximum length is not unique.

You can get rid of the complicated logic:

#include <algorithm>

//...    
if (area == 0) {
    cout << "Collinear";
}
else {
    cout << max({s1, s2, s3}));
}

Late addendum, in case this hasn't been solved yet (and I can't sleep):

The clue to solving this is in the word "integers" in the description.
The test case you're failing on has been constructed so that

(x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)

is non-zero when using floating point, and zero when using integers.

You don't need 0.5 * abs(... to determine whether the area is zero or not, and I expect the following to work:

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

int square(int x) { return x * x; }

int main () {
    int x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1;
    cin >> x2 >> y2;
    cin >> x3 >> y3;

    int area = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1);
    if (area != 0){
        int s1 = square(x1 - x2) + square(y1 - y2);
        int s2 = square(x2 - x3) + square(y2 - y3);
        int s3 = square(x1 - x3) + square(y1 - y3);
        cout << max(s1, max(s2, s3));
    }
    else
    {
        cout <<"Collinear";
    } 
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82