Fishnet(POJ_1408)
2016-04-05 15:08
253 查看
Description
A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previous night he had encountered a terrible storm and had reached this uninhabited island. Some wrecks of his ship were spread around him. He found a square wood-frame and a long thread among the wrecks. He had to survive in this island until someone came and saved him.In order to catch fish, he began to make a kind of fishnet by cutting the long thread into short threads and fixing them at pegs on the square wood-frame. He wanted to know the sizes of the meshes of the fishnet to see whether he could catch small fish as well as large ones.
The wood frame is perfectly square with four thin edges on meter long: a bottom edge, a top edge, a left edge, and a right edge. There are n pegs on each edge, and thus there are 4n pegs in total. The positions of pegs are represented by their (x,y)-coordinates. Those of an example case with n=2 are depicted in figures below. The position of the ith peg on the bottom edge is represented by (ai,0). That on the top edge, on the left edge and on the right edge are represented by (bi,1), (0,ci) and (1,di), respectively. The long thread is cut into 2n threads with appropriate lengths. The threads are strained between (ai,0) and (bi,1),and between (0,ci) and (1,di) (i=1,…,n).
You should write a program that reports the size of the largest mesh among the (n+1)2 meshes of the fishnet made by fixing the threads at the pegs. You may assume that the thread he found is long enough to make the fishnet and the wood-frame is thin enough for neglecting its thickness.
Input
The input consists of multiple sub-problems followed by a line containing a zero that indicates the end of input. Each sub-problem is given in the following format.n
a1 a2 … an
b1 b2 … bn
c1 c2 … cn
d1 d2 … dn
you may assume 0 < n <= 30, 0 < ai,bi,ci,di < 1
Output
For each sub-problem, the size of the largest mesh should be printed followed by a new line. Each value should be represented by 6 digits after the decimal point, and it may not have an error greater than 0.000001.Sample Input
20.2000000 0.6000000
0.3000000 0.8000000
0.1000000 0.5000000
0.5000000 0.6000000
2
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
4
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
2
0.5138701 0.9476283
0.1717362 0.1757412
0.3086521 0.7022313
0.2264312 0.5345343
1
0.4000000
0.6000000
0.3000000
0.5000000
0
Sample Output
0.2156570.111112
0.078923
0.279223
0.348958
代码
这题没有什么难的地方,精度问题也不用考虑,一开始担心输入数据小数点后有七位数用double不行,结果一看误差控制在0.000001。。。那就没什么可担心的了。。点的话用二维数组存起来就好,一开始想不到的话对着题目中的图再仔细想想一定能想出来的,话不多说,直接上代码#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; struct Point { double x, y; }p[33][33]; void intersection(int n, int i, int j) { Point a = p[0][i], b = p[n + 1][i], c = p[j][0], d = p[j][n + 1]; double t = ((a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x)) / ((a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x)); p[j][i] = a; p[j][i].x += (b.x - a.x) * t; p[j][i].y += (b.y - a.y) * t; } double area_polygon(int i, int j) { double ret = 0; double x1 = p[i + 1][j].x - p[i][j].x, y1 = p[i + 1][j].y - p[i][j].y; double x2 = p[i + 1][j + 1].x - p[i][j].x, y2 = p[i + 1][j + 1].y - p[i][j].y; ret += fabs(x1 * y2 - x2 * y1); x1 = p[i][j + 1].x - p[i][j].x, y1 = p[i][j + 1].y - p[i][j].y; ret += fabs(x1 * y2 - x2 * y1); return ret / 2; } int main() { int n; while(scanf("%d", &n) && n) { memset(p, 0, sizeof(p)); p[n + 1][0].x = p[0][n + 1].y = p[n + 1][n + 1].x = p[n + 1][n + 1].y = 1; for(int j = 0; j < 4; j++) { for(int i = 1; i <= n; i++) { if(j == 0) scanf("%lf", &p[i][0].x); else if(j == 1) { scanf("%lf", &p[i][n + 1].x); p[i][n + 1].y = 1; } else if(j == 2) scanf("%lf", &p[0][i].y); else { scanf("%lf", &p[n + 1][i].y); p[n + 1][i].x = 1; } } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { intersection(n, i, j); } } double ans = 0; for(int i = 0; i <= n; i++) { for(int j = 0; j <= n; j++) { ans = max(ans, area_polygon(i, j)); } } printf("%.6f\n", ans); } return 0; }
相关文章推荐
- 常用js函数
- Android安全机制
- VR头盔产品镜片评测
- mysql连接失败access denied for user root @localhost
- Log4j配置详细说明
- Net分布式系统之二:CentOS系统搭建Nginx负载均衡(下)
- 每个程序员都应该知道的 16个最佳 PHP 库
- 分布式入门之2:Quorum机制
- Thinkpad E450c开启Intel virtual technology
- Android 网络框架 volley源码剖析
- 使用MediaPlayer和SurfaceView播放视频
- Android之 如何解决ScrollView 和ListView滑动冲突的问题如何解决ScrollView can host only one direct child
- 4.4作业(变更管理+安全管理)
- 利用NSAttributeString进行图文混编
- Python:eval的妙用和滥用
- 通讯录
- C#StreamReader和StreamWriter
- Page.IsValid的使用
- CSS学习笔记(一)
- ProgressBar AsyncTask实现界面数据异步加载