1 / 3
Oct 2022

Cześć, byłby ktoś w stanie powiedzieć czemu sędzia mi nie akceptuje kodu? Sprawdzałem na wielu przypadkach i wszystko działa. Korzystam z metody obliczenia 3 pól (x1,x2,x), (x1,x,x3), (x,x2,x3). Jeżeli ich suma jest większa od pola (x1,x2,x3) to punkt jest na zewnątrz. Jeżeli jedno z nich == 0 oraz x i y znajduje się “na ramionach” trójkąta to krawędź. Ani to, ani to - wewnątrz. Coś wyczytałem, że istotna jest kolejność warunków, ale za bardzo nie wiem jaka by miała być i co to by zmieniało. Jak wam się udało rozwiązać to śmiało podsyłajcie kody.

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

double pole(double, double, double, double, double, double);

int main()
{
    int x1, y1, x2, y2, x3, y3, x, y;
    while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x >> y)
    {
	    if ((x1 == 0) && (y1 == 0) && (x2 == 0) && (y2 == 0) && (x3 == 0) && (y3 == 0) && 
                    (x == 0) && (y == 0))
		    break;
	    if ((pole((double)x, (double)y, (double)x2, (double)y2, (double)x3, (double)y3)
	    	    + pole((double)x1, (double)y1, (double)x, (double)y, (double)x3, (double)y3)
		    + pole((double)x1, (double)y1, (double)x2, (double)y2, (double)x, (double)y))
		    > pole((double)x1, (double)y1, (double)x2, (double)y2, (double)x3, (double)y3))
		            cout << "O" << endl;
	    else if (((pole((double)x, (double)y, (double)x2, (double)y2,(double)x3, (double)y3)==0)   
		           && ((x2 <= x && x <= x3) || (x3 <= x && x <= x2)) 
                               && ((y2 <= y && y <= y3) || (y3 <= y && y <= y2))
		    ) || (
	               (pole((double)x1, (double)y1, (double)x, (double)y, (double)x3, (double)y3) ==0)   
			       && ((x1 <= x && x <= x3) || (x3 <= x && x <= x1)) 
                                   && ((y2 <= y && y <= y3) || (y3 <= y && y <= y2))
		    ) || (
	              (pole((double)x1, (double)y1, (double)x2, (double)y2, (double)x, (double)y)== 0)
			       && ((x1 <= x && x <= x2) || (x2 <= x && x <= x1)) 
                                   && ((y2 <= y && y <= y3) || (y3 <= y && y <= y2))))
		               cout << "E" << endl;
	    else
		               cout << "I" << endl;
    }
}

double pole(double x1, double y1, double x2, double y2, double x3 , double y3)
{
    return (abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))/2);
}
  • created

    Oct '22
  • last reply

    Oct '22
  • 2

    replies

  • 331

    views

  • 2

    users

a przetestowałeś na danych przykładowych ? Bo mi po wklejeniu do ideone daje błędną odpowiedź już dla przykładowych danych…

Dobra, zapomniałem zmienić indeksów y przy sprawdzaniu krawędzi, kod wygląda tak i sędzia okazał się łaskawy

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

double pole(int, int, int, int, int, int);

int main()
{
	int x1, y1, x2, y2, x3, y3, x, y;
	double poleD, pole1, pole2, pole3;
	while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x >> y)
	{
		if ((x1 == 0) && (y1 == 0) && (x2 == 0) && (y2 == 0) && (x3 == 0) && (y3 == 0) && (x == 0) && (y == 0))
			break;
		poleD = pole(x1, y1, x2, y2, x3, y3);
		pole1 = pole(x, y, x2, y2, x3, y3);
		pole2 = pole(x1, y1, x, y, x3, y3);
		pole3 = pole(x1, y1, x2, y2, x, y);
		if (((pole1 == 0)
				&& (((x2 <= x && x <= x3) || (x3 <= x && x <= x2))
				&& ((y2 <= y && y <= y3) || (y3 <= y && y <= y2)))
			) || (
				(pole2 == 0)
				&& (((x1 <= x && x <= x3) || (x3 <= x && x <= x1))
				&& ((y1 <= y && y <= y3) || (y3 <= y && y <= y1)))
			) || (
				(pole3 == 0)
				&& (((x1 <= x && x <= x2) || (x2 <= x && x <= x1))
				&& ((y1 <= y && y <= y2) || (y2 <= y && y <= y1)))))
			cout << "E" << endl;
		else if ((pole1 + pole2 + pole3) > poleD)
			cout << "O" << endl;
		else
			cout << "I" << endl;
	}
}

double pole(int x1, int y1, int x2, int y2, int x3 , int y3)
{
	return (abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))/2.0);
}