Thursday, August 21, 2014

Chef and The Right Triangles


Problem Link : http://www.codechef.com/problems/RIGHTRI/

Idea : Given co-ordinate of three vertex of a triangle in 2D space . You have to determine whether it is right triangle or not . If yes , Increase counter , because there are N triangle . A triangle is right triangle if and only if it satisfies the pythagoras theorem . If its largest side is c and other are a and b . Then the relation holds for all right triangle.
                c*c=a*a+b*b

Take input and calculate its sides . Find out its largest side and cheak the above condition and increase the counter if it is right triangle . One thing is important that , the sides are given by distance formula -

                Distance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))

Here we do the sqrt operation and in condition cheaking part we are squaring it . It may occur floating point error . For that we calculate side by -

Distance = (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)

And our cheaking condition will be -

c=a+b.

The number of right triange is our answer .

My solution : http://pastebin.com/n6HhaFca

No comments:

Post a Comment