http://acm.hdu.edu.cn/showproblem.php?pid=1147
套用模板,在链表中删掉与刚输入的这个相交的线段
#include<stdio.h> #include<stdlib.h> typedef struct node { int num; double x1,x2,y1,y2; struct node *next; }S; double x1[100010],x2[100010],y1[100010],y2[100010]; S *l; int same(int a,int b)//这里是判断线段是否相交 { if(((x1[a]-x1[b])*(y2[b]-y1[b])-(y1[a]-y1[b])*(x2[b]-x1[b]))* ((x2[a]-x1[b])*(y2[b]-y1[b])-(y2[a]-y1[b])*(x2[b]-x1[b]))>0)return 0; if(((x1[b]-x1[a])*(y2[a]-y1[a])-(y1[b]-y1[a])*(x2[a]-x1[a]))* ((x2[b]-x1[a])*(y2[a]-y1[a])-(y2[b]-y1[a])*(x2[a]-x1[a]))>0)return 0; return 1; } S *init()//创建链表 { S *s; s=(S *)malloc(sizeof(S)); s->next=NULL; return s; } void insert(int b)//将第b条线段插入 { int a; S *s,*p,*q; if(b==1)//初始 { s=(S *)malloc(sizeof(S)); s->num=b; s->next=NULL; l->next=s; return ; } else { s=(S *)malloc(sizeof(S)); s->num=b; s->next=NULL; q=l; p=l->next; while(p)//将之前与第b条线段相交的所有值都删掉 if(same(p->num,b)==1) { q->next=p->next; free(p); p=q->next; } else { p=p->next;q=q->next; } q->next=s; } } void print()//输出 { S *s=l->next,*q; while(s) { if(s->next==NULL) printf("%d. ",s->num); else printf("%d, ",s->num); q=s; s=s->next; free(q); } } int main() { int i,n; while(scanf("%d",&n),n) { l=init(); for(i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1[i],&y1[i],&x2[i],&y2[i]); insert(i); } printf("Top sticks: "); print(); } return 0; }