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

这题有什么bug,大牛们帮帮看下代码。 谢谢了。。。

Posted by boys at 2007-05-04 17:52:10 on Problem 3227
#include <stdio.h>
#include <math.h>
#define N 1005

struct pos{
	double x,y;
};

pos p[N];
int n;double h;

double dis(pos a,pos b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double cross(pos a,pos b,pos c)
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

pos l_l_intp(pos p1, pos p2, pos q1, pos q2)
{
	double a, b, c, d, e, f;	pos te;
	a = p2.x-p1.x,	b = p1.y-p2.y,	c = p1.y*p2.x - p1.x*p2.y;
	d = q2.x-q1.x,	e = q1.y-q2.y,	f = q1.y*q2.x - q1.x*q2.y;
	te.x = (a*f-c*d)/(a*e-b*d);
	te.y = (c*e-b*f)/(a*e-b*d);
	return te;
}

void solve()
{
	int i,j; double k1,k2; 
	pos sta; sta.x = 0.0; sta.y = h;
	double ans = 0.0; ans += dis(p[0],p[1]);

    for(i=1;i<n;i++)
	{
        if(i&1)
		{
			k1 = (sta.y-p[i].y)/(sta.x-p[i].x);
			k2 = (p[i].y-p[i+1].y)/(p[i].x-p[i+1].x);
			if(fabs(k1-k2)<=0) {
				if(i<n-2) ans += dis(p[i+1],p[i+2]);
			}
			else if(k2-k1>0)
			{
				ans += dis(p[i],p[i+1]);
				if(i<n-2) ans += dis(p[i+1],p[i+2]);
			}
			else if(i<n-2){
				for(j = i+1;j<n-1;j+=2)
				{ 
				  if(cross(sta,p[i],p[j])<0 && cross(sta,p[i],p[j+1])>0)
				  {
					 pos cro = l_l_intp(sta,p[i],p[j],p[j+1]);
					 ans += dis(cro,p[j+1]);
					 break;
				  }
				}
				i = j;
			}
		}
	}
	printf("%.2lf\n",ans);
}

int main()
{
	int i;

	while(scanf("%d%lf",&n,&h))
	{
		if(n==0 && h==0) break;
		if(n==0 || n==1){
			printf("0.00\n");
			continue;
		}

        for(i=0;i<n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		solve();
	}
	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