poj 2653 Pick-up sticks–線段相交–vector

來源:互聯網
上載者:User
/*題意:隨即扔一些棒子,輸出在最上面的棒子(沒有那個棒子壓著它)的編號線段相交問題本體用到了  vector    注意    v.erase(remove_if(v.begin(),v.end(),ok),v.end());  用法同時    v.erase  返回的是刪除過後,下一個元素的位置,不要再for裡面用 it++  在迴圈裡面自己操作*/#include<stdio.h>#include<vector>#include<algorithm>using namespace std;const double esp=1e-8;struct point{double x,y;}; struct node{point p1,p2;int no;};vector<node>v;int n;node cc;double max(double a,double b){return a>b?a:b;}double min(double a,double b){return a<b?a:b;}double cross(point p,point s,point e){return (e.x-s.x)*(p.y-s.y)-(p.x-s.x)*(e.y-s.y);}bool segintersect(point s1,point e1,point s2,point e2){return((max(s1.x,e1.x)>=min(s2.x,e2.x))&&(max(s2.x,e2.x)>=min(s1.x,e1.x))&&(max(s1.y,e1.y)>=min(s2.y,e2.y))&&(max(s2.y,e2.y)>=min(s1.y,e1.y))&&(cross(s1,s2,e2)*cross(e1,s2,e2)<=esp)&&(cross(s2,s1,e1)*cross(e2,s1,e1)<=esp)); }bool ok(node n){if(segintersect(n.p1,n.p2,cc.p1,cc.p2))return true;return false;}int main(){int i;while(scanf("%d",&n),n){v.erase(v.begin(),v.end());for(i=1;i<=n;i++){scanf("%lf%lf%lf%lf",&cc.p1.x,&cc.p1.y,&cc.p2.x,&cc.p2.y);cc.no=i;v.erase(remove_if(v.begin(),v.end(),ok),v.end());v.push_back(cc);}printf("Top sticks: ");for(vector<node>::iterator it = v.begin(); it!= v.end(); it++){printf("%d",it->no);if(it!=v.end()-1)printf(", ");else printf(".\n");}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.