Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1604 | Accepted: 1026 |
Description
Input
Output
Sample Input
2 0.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.215657 0.111112 0.078923 0.279223 0.348958
Source
点的输入顺序【周边上点,都是按照从小到大的顺序输入的】
存储每一个点【周边的+线段交点】,
然后依次遍历每一个四边形的面积
/**************************************************************************** C Accepted 208 KB 16 ms C++ 2155 B 题意: 在直角坐标系中,把第一象限的那个单位面积的正方形分成 n*n 个小四边形, 求最大四边形面积 注意:点的输入顺序【周边上点,都是按照从小到大的顺序输入的】 算法:枚举+线段求交点+叉积求面积 思路:存储每一个点【周边的+交点】, 然后依次遍历每一个四边形的面积 ******************************************************************************/ #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 40; struct Point{ double x,y; Point() {} Point(double _x, double _y){ x = _x; y = _y; } Point operator + (const Point &B) const { return Point(x+B.x, y+B.y); } Point operator - (const Point &B) const { return Point(x-B.x, y-B.y); } Point operator * (const double &p) const { return Point(p*x, p*y); } }p[maxn][maxn]; typedef Point Vector; /** 叉积求面积 */ double Cross(Point A, Point B) { return A.x*B.y - A.y*B.x; } /** 求线段交点 */ Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){ Vector u = P-Q; double t = Cross(w, u) / Cross(v, w); return P+v*t; } /** 根据四个点用叉积求四边形面积 */ double Area(Point a, Point b, Point c, Point d){ return fabs(Cross(c-a,b-a)) / 2.0 + fabs(Cross(c-a,d-a)) / 2.0; } int main() { int n; while(scanf("%d", &n) != EOF) { if(n == 0) break; p[0][0] = Point(0,1); //定位四个顶点 p[0][n+1] = Point(1,1); p[n+1][0] = Point(0,0); p[n+1][n+1] = Point(1,0); double a,b,c,d; //依次存储周边的点 for(int i = 1; i <= n; i++) //a { scanf("%lf", &a); p[n+1][i] = Point(a,0); } for(int i = 1; i <= n; i++)// b { scanf("%lf", &b); p[0][i] = Point(b,1); } for(int i = n; i >= 1; i--) //c { scanf("%lf", &c); p[i][0] = Point(0,c); } for(int i = n; i >= 1; i--) //d { scanf("%lf", &d); p[i][n+1] = Point(1,d); } //求中间的交点 for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { p[i][j] = GetLineIntersection(p[i][0], p[i][0]-p[i][n+1], p[n+1][j], p[n+1][j]-p[0][j]); } } double ans = 0; double tmp; //从上到下、从左到右依次遍历每个四边形 for(int i = 0; i <= n; i++) { for(int j = 0; j <= n; j++) { tmp = Area(p[i][j],p[i][j+1],p[i+1][j+1],p[i+1][j]); ans = max(ans,tmp); } } printf("%.6lf\n", ans); } return 0; }