萌新求助90pts
查看原帖
萌新求助90pts
171851
巴菲特脆脆鲨楼主2021/7/5 16:54

一直卡在 #4。

read 1, expect 3

所以有什么情况会把边界上误判成界内呢?

有没有大佬愿意帮我看看哪里写锅了QwQ

拜托各位巨佬了嘤



#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<iostream>
#include<algorithm>
#include<functional>
#define ll long long
#define eps 1e-10
using namespace std;

#ifndef DONLINE_JUDGE
char xch, xB[1 << 15], *xS = xB, *xTT = xB;
#define getc() (xS == xTT && (xTT = (xS = xB) + fread(xB, 1, 1 << 15, stdin), xS == xTT) ? 0 : *xS++)
#endif

#ifndef DBUFFETT
#define getc() getchar()
#endif

int rd(){
	int res = 0, fl = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-') fl = -1;
		c = getchar();
	}
	while(isdigit(c)){
		res = (res << 3) + (res << 1) + c - '0';
		c = getchar();
	}
	return res * fl;
}



string in;
struct Point{
    double x, y;
    Point operator + (const Point a) const{
        return (Point){x + a.x, y + a.y};
    }
    friend Point operator / (Point a, double b){
        return (Point){a.x / b, a.y / b};
    }
    friend Point operator - (const Point a, const Point b){
        return (Point){a.x - b.x, a.y - b.y};
    }
    friend Point operator * (const Point a, const double b){
        return (Point){a.x * b, a.y * b};
    }
    friend bool operator == (const Point a, const Point b){
        return a.x == b.x && a.y == b.y; 
    }
    friend bool operator != (const Point a, const Point b){
        return a.x != b.x || a.y != b.y;
    }
}A, B, C, D, G, Mab, Mbc, Mca, Inter;
struct line{
    Point st, ed;
}AMbc, BMca, CMab, AB, BC, CA;
double CP(Point a, Point b){
    return a.x * b.y - a.y * b.x;
}
Point GetInter(line L1, line L2){
    
    double a1 = L1.st.y - L1.ed.y, b1 = L1.ed.x - L1.st.x, c1 = L1.st.x * L1.ed.y - L1.ed.x * L1.st.y;
    double a2 = L2.st.y - L2.ed.y, b2 = L2.ed.x - L2.st.x, c2 = L2.st.x * L2.ed.y - L2.ed.x * L2.st.y;
    return Point{(c1 * b2 - c2 * b1) / (a2 * b1 - a1 * b2), (a2 * c1 - a1 * c2) / (a1 * b2 - a2 * b1)}; 
}
int InterAB, InterBC, InterCA, DAB, DBC, DCA;
int judge(Point P, line L){//0代表点在直线上,-1 1分别是直线的两边 
    double a, c;//b = 1
    a = (L.st.y - L.ed.y) / (L.ed.x - L.st.x);
    c = (L.ed.y * L.st.x - L.st.y * L.ed.x) / (L.ed.x - L.st.x);
    int val = a * P.x + P.y + c;
    if(val < 0) return -1;
    else if(val == 0)   return 0;
    else return 1;
}
int main(){
    A.x = rd();
    A.y = rd();
    B.x = rd();
    B.y = rd();
    C.x = rd();
    C.y = rd();
    D.x = rd(); 
    D.y = rd();
    Mab = (A + B) / 2;//A和B的中点记作Mab,以下同理 
    Mbc = (B + C) / 2;
    Mca = (C + A) / 2;
    AMbc.st = A;//A和Mab确定的直线,即为一条中线以下同理 
    AMbc.ed = Mbc;
    BMca.st = B;
    BMca.ed = Mca;
    Inter = GetInter(AMbc, BMca);//中线的交点记作Inter,一定在三角形内部 
    AB.st = A; AB.ed = B;//直线AB 
    BC.st = B; BC.ed = C;
    CA.st = C; CA.ed = A;
    InterAB = judge(Inter, AB);//点与直线的位置关系 
    InterBC = judge(Inter, BC);
    InterCA = judge(Inter, CA);
    DAB = judge(D, AB);
    DBC = judge(D, BC);
    DCA = judge(D, CA);
    if(D == A || D == B || D == C){
        printf("4\n");
    }
    else if((DAB == 0 && DBC == InterBC && DCA == InterCA) || (DBC == 0 && DCA == InterCA && DAB == InterAB) || (DCA == 0 && DAB == InterAB && DBC == InterBC)){
        printf("3\n");
    }
    else if(DAB != InterAB || DBC != InterBC || DCA != InterCA){
        printf("2\n");
    }
    else printf("1\n");
	return 0;
}
2021/7/5 16:54
加载中...