Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么用宏定义判断最大最小值 会出错呢?

Posted by Cunch at 2010-10-21 22:53:11 on Problem 1410

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;


int min(int x, int y){
    return x>y ? y : x;
}

int max(int x, int y){
    return x>y ? x : y;
}


//为什么用下面这个宏定义就错了呢????????
#define mmax(x,y) x>y ? x : y
#define mmin(x,y) x<y ? x : y

struct P{
    int x,y;
    P(const int _x, const int _y)
        :x(_x),y(_y) {}
    P(){}
    const P& input(){
        scanf("%d %d",&x,&y);
        return *this;
    }
    const P& output() const{
        printf("%d %d\n",x,y);
        return *this;
    }
};

int cross(const P &p1, const P &p2, const P &p3){
    return (p3.x - p1.x)*(p2.y - p1.y) - (p3.y - p1.y)*(p2.x - p1.x);
}

bool online(const P &p1, const P &p2, const P &p3, int flag =0){

    if(min(p1.x, p2.x) <= p3.x && p3.x <= max(p1.x, p2.x) && min(p1.y, p2.y) <= p3.y && p3.y <= max(p1.y, p2.y)){
     //   printf("%d %d %d\n",min(p1.x, p2.x),max(p1.x, p2.x),p3.x);
     //   printf("%d %d %d\n",min(p1.y, p2.y),max(p1.y, p2.y),p3.y);
        return true;
    }
    /*
    if(mmin(p1.x, p2.x) <= p3.x && p3.x <= mmax(p1.x, p2.x) && mmin(p1.y, p2.y) <= p3.y && p3.y <= mmax(p1.y, p2.y)){
     //   printf("%d %d %d\n",min(p1.x, p2.x),max(p1.x, p2.x),p3.x);
     //   printf("%d %d %d\n",min(p1.y, p2.y),max(p1.y, p2.y),p3.y);
        return true;
    }*/
    return false;
}

bool lineInsect(const P &p1, const P &p2, const P &p3, const P &p4){
    int d1 = cross(p1, p2, p3);
    int d2 = cross(p1, p2, p4);
    int d3 = cross(p3, p4, p1);
    int d4 = cross(p3, p4, p2);

    if(d1*d2 < 0 && d3*d4<0) return true;
    else if(d1 ==0 && online(p1, p2, p3)) return true;
    else if(d2 ==0 && online(p1, p2, p4)) return true;
    else if(d3 ==0 && online(p3, p4, p1)) return true;
    else if(d4 ==0 && online(p3, p4, p2)) return true;
    return false;
}

int main(){
    P a,b,r1,r2,r3,r4;
    int tt;
    //freopen("in.txt","r",stdin);
    //freopen("out2.txt","w",stdout);
    scanf("%d",&tt);
    while(tt--){
        scanf("%d%d",&a.x,&a.y);
        scanf("%d%d",&b.x,&b.y);
        scanf("%d%d",&r1.x,&r1.y);
        scanf("%d%d",&r3.x,&r3.y);

        P tp;
        if(r1.x > r3.x){
            tp = r1;
            r1 = r3;
            r3 = tp;
        }

        r2 = P(r3.x, r1.y);
        r4 = P(r1.x, r3.y);

      //  r1.output();
       // r2.output();
       // r3.output();
       // r4.output();

        bool isInsect = false;
        if(lineInsect(a,b,r1,r2)) {
           isInsect = true;
        }
        if(!isInsect && lineInsect(a,b,r2,r3)){
            isInsect = true;
        }
        if(!isInsect && lineInsect(a,b,r3,r4)){
            isInsect = true;
        }
        if(!isInsect && lineInsect(a,b,r4,r1)){
            isInsect = true;
        }


        P tp1, tp2;
        tp1.x = min(r1.x,r3.x);
        tp1.y = max(r1.y,r3.y);

        tp2.x = max(r1.x,r3.x);
        tp2.y = min(r1.y,r3.y);

        //printf("%d %d %d %d %d %d\n",r1.x,r1.y,r3.x,r3.y,a.x,a.y);
        if(tp1.x <= a.x && a.x <= tp2.x && tp2.y <= a.y && a.y <= tp1.y){
            isInsect = true;
        }

        if(isInsect) printf("T\n");
        else printf("F\n");
    }
    return 0;
}




Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator