題目也就是讓我找到在最上面的棍子。
枚舉棍子i上方的每根棍子j(i+1≤j≤n),若其中任何一根棍子與棍子i相交,則說明棍子i被上面的棍子壓住了,直接判斷棍子i+1;若棍子i未與上方的任何一根棍子相交,則棍子i屬於最上面的棍子。
#include<cstdio>
#include<cmath>
#include<algorithm>
const int maxn = 1e5+10;
const int epsi = 1e-10;
struct point{
double x,y;
point(){};
point(double x,double y) :x(x),y(y){};
point operator - (const point &p2)const{
return point(x-p2.x,y-p2.y);
}
double operator ^(const point &p2)const{
return (x*p2.y)-(y*p2.x);
}
}p1[maxn],p2[maxn];
inline int sign(const double &x){
if(x>epsi) return 1;
if(x<-epsi) return -1;
return 0;
}
inline double mul(const point &p0,const point &p1,const point &p2){
return (p1-p0) ^ (p2-p0);
//return (p1-p0) ^(p2-p1);
}
inline int cross(const point &p0,const point &p1,const point &p2,const point &p3){
double a1 = mul(p0,p1,p2),a2=mul(p0,p1,p3);
//double a1 = mul(p0,p2,p1),a2 = mul(p0,p2,p3);
if(sign(a1)==0 && sign(a2)==0) return 2;
if(sign(a1) == sign(a2)) return 0;
return 1;
}
int main(){
int n;
while(~scanf("%d",&n) && n){
printf("Top sticks:");
bool f1 = false;
for(int i = 1 ;i <= n;i ++)
scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
for(int i = 1;i <= n;i ++){
bool flag = false;
for(int j=i+1;j <= n;j++){
if(cross(p1[i],p2[i],p1[j],p2[j])==1 && cross(p1[j],p2[j],p1[i],p2[i])==1){
flag = true;break;
}
}
if(flag==false && f1==true) printf(",");
if(flag==false) printf(" %d",i),f1=true;
}
printf(".\n");
}
return 0;
}