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. 【转载】Python中的正则表达式教程

    本文http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 正则表达式经常被用到,而自己总是记不全,转载一份完整的以备不时之需. 1. ...

  2. Android破解学习之路(五)——Android游戏 割绳子:魔法 + 在游戏加入Toast弹窗提示

    前言:这一期的破解教程,有新的知识内容出现啦! 这一期破解的游戏是找不到之前的关键字,怎么破解呢? 破解成功之后,添加一个Toast弹窗提示由XX破解,这操作该如何实现呢?请往下看~ 链接: http ...

  3. 基于阿里云的MQTT远程控制

    好久没有写博客了,眼看自己的项目就要快做完了,先分享一下基于MQTT的远程控制,自己买了一个阿里的云端,然后在云端上安装了一个MQTT服务器,其实是一不小心买了两个,所以准备贡献出来一个供大家使用, ...

  4. IT服务(运维)管理实施的几个要点--序言

    IT服务(运维)管理(不是IT运维技术)是IT行业当中相对比较"窄"的一个分支,通常只被金融.电信等大型数据中心的中高层管理人员所关注.但是根据笔者多年从事IT服务和服务管理的经验 ...

  5. ABP 教程文档 1-1 手把手引进门之 AngularJs, ASP.NET MVC, Web API 和 EntityFramework(官方教程翻译版 版本3.2.5)含学习资料

    本文是ABP官方文档翻译版,翻译基于 3.2.5 版本 转载请注明出处:http://www.cnblogs.com/yabu007/  谢谢 官方文档分四部分 一. 教程文档 二.ABP 框架 三. ...

  6. tomcat中使用mysql连接池的配置

    1.下载相应的jar包,添加到工程中 需要下载的包主要有commons-pool2-2.2 commons-dbcp2-2.0.1-src commons-dbcp2-2.0.1  commons-c ...

  7. Python 阿里大于发送手机验证码

    1.安装阿里大于的包 pip install top 2.事例脚本 # -*- coding: utf-8 -*- import top.api appkey = '2353xxxx' secret ...

  8. python实现散列表的直接寻址法

    散列表(Hash table,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构.也就是说,它通过计算一个关于键值的函数, 将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速 ...

  9. js 数组API之forEach、map的用法

    forEach语法: arr.forEach(function(value, index, array){--}) 实例: // forEach ,,,,]; arr.forEach(function ...

  10. CommonJS, AMD ,CMD之间的关系

    commonjs是用在服务器端的,同步的,如nodejs amd, cmd是用在浏览器端的,异步的,如requirejs和seajs 其中,amd先提出,cmd是根据commonjs和amd基础上提出 ...