Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
不明白为什么这样做会错,哪位路过的好心人帮我看看,给我个数据吧~~~~~#include<stdio.h> #include<math.h> #include<iostream> using namespace std; typedef struct point { double x,y; } Point; Point p[1001]; //功能:求点在有向线段(a → b)左边还是右边 //返回:0共线、1左边、-1右边 int JudgeSide(Point b,Point a,double x,double y) { double sum1,sum2; sum1=(y-a.y)*(x-b.x); sum2=(y-b.y)*(x-a.x); return((sum1<sum2)?-1:((sum1>sum2)?1:0)); } //求直线p11p12与直线p21p22的交点,结果返回到ans void Intersection(Point p11,Point p12,Point p21,Point p22,Point &ans) { double k1,k2; k1 = (p11.y-p12.y)/(p11.x-p12.x); k2 = (p21.y-p22.y)/(p21.x-p22.x); ans.x = (k1*p11.x-k2*p21.x+p21.y-p11.y)/(k1-k2); ans.y = k1*(ans.x-p11.x)+p11.y; } //求两点距离 double len(Point a,Point b) { return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)); } //调整看不到的点,将该点调整到刚好能看到的地方 void getnext(double h,int th) { int i,j; Point tmp,ans; tmp.x = 0; tmp.y = h; Intersection(tmp,p[th-1],p[th],p[th+1],ans); p[th].x = ans.x; p[th].y = ans.y; } int main() { double h,sum; int n,i,j; freopen("data.txt","r",stdin); while(scanf("%d%lf",&n,&h)==2&&(n!=0||h>0)) { sum=0.0; for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); for(i=1;i<n;i++) { int t=JudgeSide(p[i-1],p[i],0.0,h); if(t==1) sum += len(p[i-1],p[i]); else if(t==-1) getnext(h,i); } printf("%.2f\n",sum); } } /*数据 9 5 0 0 2 5 4 1 5 2 6 1 8 8 10 2 11 3 12 0 7 6 0 0 2 5 3 2 4 3 100 1 101 6 102 0 5 0 0 0 1 2 3 1 4 100 5 0 结果 8.51 104.42 93.90 */ Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator