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

哎 谁有聊能帮看下程序 wa爽了 注释已加

Posted by kofrong at 2009-08-13 20:12:25 on Problem 2826
#include<iostream>
#include<cmath>
#include<stdio.h>
#include<algorithm>

using namespace std;
#define eps 1e-12
#define zero(x) (((x)>0?(x):-(x))<eps)

struct Point
{
    double x,y;
};
struct Line
{
    Point a,b;
};

double xmult(Point p1,Point p2,Point p0)//叉×
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dmult(Point p1,Point p2,Point p0)//点乘
{
    return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}
int dot_online_in(Point p,Line l)//判断点是否在线段上
{
    return zero(xmult(p,l.a,l.b))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps;
}
int parallel(Line u,Line v)//判断是否平行
{
    return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));
}
Point intersection(Line u,Line v)//求2直线的交点
{
    Point ret=u.a;
    double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
    ret.x+=(u.b.x-u.a.x)*t;
    ret.y+=(u.b.y-u.a.y)*t;
    return ret;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        Point tmp;//记录2直线最高点较低的点

        Point ko;//求平行直线后得到的交点
        Line pall;//最后求平行x轴直线时求的直线
        double ans=0;
        double x1,x2,y1,y2;
        scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);//读入数据时把Y轴坐标高的放B点
        if(y1>y2)
        {
            swap(y1,y2);
            swap(x1,x2);
        }
        Line l1,l2;
        l1.a.x=x1,l1.a.y=y1,l1.b.x=x2,l1.b.y=y2;
        scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
        if(y1>y2)
        {
            swap(y1,y2);
            swap(x1,x2);
        }

        l2.a.x=x1,l2.a.y=y1,l2.b.x=x2,l2.b.y=y2;
        if(parallel(l1,l2))//如果平行就无解退出
        {
            printf("0.00\n");
        }
        else
        {
            bool flag=true;//标记是否有垂直于x轴的边
            Point p=intersection(l1,l2);
            if(dot_online_in(p,l1)&&(dot_online_in(p,l2)))//如果2直线的交点都不在直线上就退出
            {
                double kl1,kl2;//l1, l2的斜率
                if(l1.b.x-l1.a.x==0||l2.b.x-l2.a.x==0)//如果出现斜率无解就不计算斜率
                {
                    flag=false;
                }
                else
                {
                    kl1=(l1.b.y-l1.a.y)/(l1.b.x-l1.a.x);
                    kl2=(l2.b.y-l2.a.y)/(l2.b.x-l2.a.x);
                }
                if(flag&&kl1<0&&kl2<0&&l1.b.y>=l2.b.y&&l1.b.x<=l2.b.x)//下面4个if判断是否出现上板比下版长的情况
                {
                    printf("0.00\n");
                }
                else if(flag&&kl1>0&&kl2>0&&l1.b.y>=l2.b.y&&l1.b.x>=l2.b.x)
                {
                    printf("0.00\n");
                }
                else if(flag&&kl2<0&&kl1<0&&l2.b.y>=l1.b.y&&l2.b.x<=l1.b.x)
                {
                    printf("0.00\n");
                }
                else if(flag&&kl2>0&&kl1>0&&l2.b.y>=l1.b.y&&l2.b.x>=l1.b.x)
                {
                    printf("0.00\n");
                }
                else if(l1.a.y==l1.b.y||l2.a.y==l2.b.y)
                {
                    printf("0.00\n");
                }
                else//当都满足时开始计算
                {
                    if(l1.b.y<l2.b.y)
                    {
                        tmp=l1.b;
                        pall.a=tmp;
                        pall.b.x=0;
                        pall.b.y=tmp.y;
                        ko=intersection(pall,l2);
                    }
                    else
                    {
                        tmp=l2.b;
                        pall.a=tmp;
                        pall.b.x=0;
                        pall.b.y=tmp.y;
                        ko=intersection(pall,l1);
                    }
                    //if(ko.x!=tmp.x)
                    {
                        ans=abs(ko.x-tmp.x)*(tmp.y-p.y)/2;
                    }
                    //else
                    //{
                        //printf("0.00")
                    printf("%.2lf\n",ans);
                }

            }
            else
            {
                printf("0.00\n");
            }
            //cout<<p.x<<p.y<<endl;
        }

        //cout<<x1<<y1<<x2<<y2<<endl;
    }
    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