http://poj.org/problem?id=2826
An Easy Problem?!
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10505   Accepted: 1584

Description

It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails two wooden boards on the wall of his barn. Shown in the pictures below, the two boards on the wall just look like two segments on the plane, as they have the same width. 

Your mission is to calculate how much rain these two boards can collect. 

Input

The first line contains the number of test cases. 
Each test case consists of 8 integers not exceeding 10,000 by absolute value, x1y1x2y2x3y3x4y4. (x1y1), (x2y2) are the endpoints of one board, and (x3y3), (x4y4) are the endpoints of the other one. 

Output

For each test case output a single line containing a real number with precision up to two decimal places - the amount of rain collected. 

Sample Input

2
0 1 1 0
1 0 2 1 0 1 2 1
1 0 1 2

Sample Output

1.00
0.00 题解:判断可以接多少水,如果两直线可以接水的话,求三角形面积
下面给出一些计算几何最基础的结论(常用)
两向量点乘: a*b*cosO = x1*y1+x2*y2 用来 1 :计算夹角 2:计算投影
两向量叉乘: a*b*sinO = x1*y1-x2*y2 用来 1:计算面积 2:判断点在直线的哪边,右手法则
这个题输出的时候要注意,用G++交题的时候要最后输出ans+eps;
下面是代码:
 #include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; #define N 105
#define eps 1e-6//精度太小会导致出错,精度太大会增加计算量,自己权衡
struct point {
double x, y;
point(){}
point(double x, double y):x(x), y(y){}
point operator + (const point o) const{
return point(x+o.x, y+o.y);
}
point operator - (const point o) const{
return point(x-o.x, y-o.y);
} double operator * (const point o) const{
return x*o.y - o.x*y;
} double operator ^ (const point o) const{//点乘
return x*o.x + y*o.y;
} point operator * (const double a) const{
return point(a*x, a*y);
} double len2()
{
return x*x + y*y;
}
}a, b, s, t; point Intersection(point a, point b, point c, point d)
{
double t = ((d - a)*(c - a))/((b - a)*(d - c));
return a + (b-a)*fabs(t);
} int main()
{
point a, b, c, d;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y);
scanf("%lf %lf %lf %lf", &c.x, &c.y, &d.x, &d.y); if(fabs(a.y - b.y) < eps || fabs(c.y - d.y) < eps)//有水平线
{
puts("0.00");
continue;
} if(a.y < b.y) swap(a, b);
if(c.y < d.y) swap(c, d); if(fabs((b.y-a.y)*(d.x-c.x) - (d.y-c.y)*(b.x-a.x)) < eps) //两条线平行
{
puts("0.00");
continue;
} if(((b-a)*(c-a))*((b-a)*(d-a)) > || ((d-c)*(a-c))*((d-c)*(b-c)) > )//两条线段根本没有交点
{
puts("0.00");
continue;
}
point p = Intersection(a, b, c, d);
point up = point(,);
if(((a-p)*(up)) * ((c-p)*(up)) > )//能收集雨水的部分在竖直线的同一侧
{
if((a-p)*(c-p) > && c.x- a.x>= -eps)
{
puts("0.00");
continue;
}
if((a-p)*(c-p) < && a.x- c.x>= -eps)
{
puts("0.00");
continue;
}
}
point t1, t2;
t1.y = t2.y = min(a.y, c.y);
t1.x = a.x + (b.x - a.x)*(t1.y - a.y)/(b.y-a.y);
t2.x = c.x + (d.x - c.x)*(t2.y - c.y)/(d.y-c.y);
double ans = fabs((t1.x - t2.x) * (t1.y-p.y)/2.0);
printf("%.2f\n", ans+eps); //控制精度,
}
return ;
}

An Easy Problem?!(细节题,要把所有情况考虑到)的更多相关文章

  1. HDU 5475:An easy problem 这题也能用线段树做???

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. HDOJ(HDU) 2123 An easy problem(简单题...)

    Problem Description In this problem you need to make a multiply table of N * N ,just like the sample ...

  3. UESTC 1591 An easy problem A【线段树点更新裸题】

    An easy problem A Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

  4. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  5. HDOJ(HDU) 2132 An easy problem

    Problem Description We once did a lot of recursional problem . I think some of them is easy for you ...

  6. Codeforces Round #392 (Div. 2)-758D. Ability To Convert(贪心,细节题)

    D. Ability To Convert time limit per test 1 second Cmemory limit per test 256 megabytes input standa ...

  7. CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?

    CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...

  8. hdu2601 An easy problem(数学)

    题目意思: http://acm.hdu.edu.cn/showproblem.php? pid=2601 给出一个数N,求N=i*j+i+j一共同拥有多少种方案. 题目分析: 此题直接暴力模拟就可以 ...

  9. [poj 2453] An Easy Problem

    An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8371   Accepted: 5009 D ...

随机推荐

  1. 单源最短路径(1):Dijkstra 算法

    一:背景 Dijkstra 算法(中文名:迪杰斯特拉算法)是由荷兰计算机科学家 Edsger Wybe Dijkstra 提出.该算法常用于路由算法或者作为其他图算法的一个子模块.举例来说,如果图中的 ...

  2. java 设计模式-缺省适配器模式

    本文转载地址:http://www.cnblogs.com/iyangyuan/archive/2013/03/11/2954808.html 在程序设计过程中,读者很可能遇到这样一种困境:设计了一个 ...

  3. Qt--自定义Delegate

    这是Model/View中的最后一篇了,Qt官方显然弱化了Controller在MVC中的作用,提供了一个简化版的Delegate:甚至在Model/View框架的使用中,提供了默认的委托,让这个控制 ...

  4. MAMP升级mysql5.6到5.7

    RT 1.先把mamp环境停掉 sudo sh /Applications/MAMP/bin/stop.sh 2.然后使用brew安装mysql5.7 brew install mysql 3.默认安 ...

  5. Windows as a Service(3)——使用SCCM管理Windows10更新

    Hello 小伙伴们,这是这个系列的第三篇文章,我已经和大家分享了有关于Windows 10服务分支以及利用WSUS管理更新的方式,有兴趣的小伙伴们可以参考下面的链接: Windows as a Se ...

  6. 【读书笔记】《Effective Java》——目录

    第二章——创建和销毁对象 第1条:考虑用静态工厂方法替代构造器 第2条:遇到多个构造器参数时要考虑用构建器 第3条:用私有构造器或者枚举类型强化Singleton属性 第4条:通过私有构造器强化不可实 ...

  7. Python安装和开发环境搭建

    1.官网:http://www.python.org/download/下载安装包,目前最新版本为3.6,安装包很多地方可以下,也可以在360软件管家上下载安装  特别要注意勾选:Add Python ...

  8. javascript中name,value等属于保留字

    前几天在练习js代码的时候,碰到了一个坑,这是让人醉了. html代码如下: <div> <div> <!--输入 123456--> <lable>请 ...

  9. Flask快速入门,知识整理

    一.Flask介绍(轻量级的框架,非常快速的就能把程序搭建起来) Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是 ...

  10. 单独mybatis得使用

    今天同学说要学习mybatis后来他写了个程序让我看看,我看了一下发现包引错了,他写的是单独的mybatis,引入的却是spring-mybatis,所以会报错. 今天我记录一下单独mybatis的使 ...