TJU : 3513 (Triangle)



// http://acm.tju.edu.cn/toj/showp3513.html
// Tag: geometry 

// Link :
// Runtime : 0.00s
// Memory : 1216K
// Tag : geometry

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL unsigned long long
using namespace std;

int X1, Y1, X2, Y2, X3, Y3;
int dist1, dist2, dist3;
int iso_sca;    // 1 = iso,  2 = sca
int ac_ob_ri;   // 1 = ac,  2 = ob, 3 = ri

int p2 (int p) { return p * p; }

void isosceles_or_scalene ()
{
    dist1 = p2 (X1 - X2) + p2 (Y1 - Y2); // (X1, Y1) and (X2, Y2)
    dist2 = p2 (X2 - X3) + p2 (Y2 - Y3); // (X2, Y2) and (X3, Y3)
    dist3 = p2 (X1 - X3) + p2 (Y1 - Y3); // (X1, Y1) and (X3, Y3)

    iso_sca = 0;

    if ( dist1 == dist2 || dist1 == dist3 || dist2 == dist3 ) {
        iso_sca = 1; return;
    }
    else if ( dist1 != dist2 && dist1 != dist3 && dist2 != dist3 )
        iso_sca = 2;
}

void acute_obtuse_or_right ()
{
    double deg1 = acos ( (dist1 + dist2 - dist3) / (2.0 * sqrt (dist1) * sqrt (dist2)) );
    double deg2 = acos ( (dist3 + dist2 - dist1) / (2.0 * sqrt (dist3) * sqrt (dist2)) );
    double deg3 = acos ( (dist1 + dist3 - dist2) / (2.0 * sqrt (dist1) * sqrt (dist3)) );

    ac_ob_ri = 0;

    deg1 *= (180 / pi);
    deg2 *= (180 / pi);
    deg3 *= (180 / pi);

    if ( deg1 < 1e-6 || deg2 < 1e-6 || deg3 < 1e-6 ) ac_ob_ri = 0;
    else if ( deg1 > 90 || deg2 > 90 || deg3 > 90 ) ac_ob_ri = 2;
    else if ( deg1 < 90 && deg2 < 90 && deg3 < 90 ) ac_ob_ri = 1;
    else if ( deg1 - 90 < 1e-6 || deg2 - 90 < 1e-6 || deg3 - 90 < 1e-6 ) ac_ob_ri = 3;
}

int main ()
{
    int testCase;
    scanf ("%d", &testCase);
    int cases = 0;

    while ( testCase-- ) {
        scanf ("%d %d %d %d %d %d", &X1, &Y1, &X2, &Y2, &X3, &Y3);

        printf ("Case #%d: ", ++cases);

        isosceles_or_scalene ();

        acute_obtuse_or_right ();

        if (iso_sca && ac_ob_ri) {
            if ( iso_sca == 1 ) printf ("isosceles");
            else printf ("scalene");

            if ( ac_ob_ri == 1 ) printf (" acute");
            else if ( ac_ob_ri == 2 ) printf (" obtuse");
            else printf (" right");
        }
        else printf ("not a");

        printf (" triangle\n");

    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

2 thoughts on “TJU : 3513 (Triangle)

  1. whats wrong with my code below ??? i’m getting WA. can you give me some hint?

    #include
    #include
    #include
    using namespace std;

    long test,t;

    main()
    {
    double AB,AC,BC,cosA,cosB,cosC,x1,x2,x3,y1,y2,y3,a[3],pi;
    pi=2*acos(0.0);
    scanf(“%ld”,&test);
    for (t=1;t<=test;t++)
    {
    scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);

    AB=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    BC=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
    AC=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));

    a[0]=AB;
    a[1]=BC;
    a[2]=AC;

    sort(a,a+3);

    if ( a[2]<a[1]+a[0])
    {
    cosA=acos((AB*AB+AC*AC-BC*BC)/(2*AB*AC));
    cosA=(180*cosA)/pi;
    cosB=acos((AB*AB-AC*AC+BC*BC)/(2*AB*BC));
    cosB=(180*cosB)/pi;
    cosC=acos((-AB*AB+AC*AC+BC*BC)/(2*BC*AC));
    cosC=(180*cosC)/pi;

    // printf("%lf %lf %lf\n\n",cosA,cosB,cosC);

    if ( AB==AC || AB==BC || AC==BC )
    {
    if ( fabs(cosA-90)<1e-6 || fabs(cosB-90)<1e-6 || fabs(cosC-90)90 || cosB>90 || cosC>90 )
    printf(“Case #%ld: isosceles obtuse triangle\n”,t);
    else if ( cosA<90 && cosB<90 && cosC<90 )
    printf("Case #%ld: isosceles acute triangle\n",t);
    }

    else
    {
    if ( fabs(cosA-90)<1e-6 || fabs(cosB-90)<1e-6 || fabs(cosC-90)90 || cosB>90 || cosC>90 )
    printf(“Case #%ld: scalene obtuse triangle\n”,t);
    else if ( cosA<90 && cosB<90 && cosC<90 )
    printf("Case #%ld: isosceles acute triangle\n",t);
    }

    }
    else
    printf ("Case #%ld: not a triangle\n",t);
    }

    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s