标签:
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
题意很简单,在1*1的正方形块中每条边有n个点,对应边的对应点连线,将正方形分割成(n+1)*(n+1)个小正方形,求出小正方形中的最大的面积。
先储存下每个点的坐标,然后用叉积计算出各个小正方形的面积,找出最大值。
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std ; #define eps 1e-8 struct node { double x , y ; } p[40][40]; double a[40] , b[40] , c[40] , d[40] ; node f(node a1,node a2,node b1,node b2) { node q = a1 ; double t = ( (a1.x-b1.x)*(b1.y-b2.y) - (a1.y-b1.y)*(b1.x-b2.x) ) / ( (a1.x-a2.x)*(b1.y-b2.y) - ( a1.y-a2.y )*(b1.x-b2.x) ) ; q.x += (a2.x-a1.x)*t ; q.y += ( a2.y-a1.y )*t ; return q ; } int main() { int n , i , j ; double k , max1 , ans ; while( scanf("%d", &n) && n ) { p[0][0].x = p[0][0].y = 0.0 ; for(i = 1 ; i <= n ; i++) { scanf("%lf", &a[i]) ; p[0][i].x = a[i] ; p[0][i].y = 0.0 ; } p[0][n+1].x = 1.0 ; p[0][n+1].y = 0.0 ; p[n+1][0].x = 0 ; p[n+1][0].y = 1.0 ; for(i = 1 ; i <= n ; i++) { scanf("%lf", &b[i]) ; p[n+1][i].x = b[i] ; p[n+1][i].y = 1.0 ; } p[n+1][n+1].x = p[n+1][n+1].y = 1.0 ; for(i = 1 ; i <= n ; i++) { scanf("%lf", &c[i]) ; p[i][0].x = 0.0 ; p[i][0].y = c[i] ; } for(i = 1 ; i <= n ; i++) { scanf("%lf", &d[i]) ; p[i][n+1].x = 1.0 ; p[i][n+1].y = d[i] ; } for(i = 1 ; i <= n ; i++) for(j = 1 ; j <= n ; j++) { if( fabs( c[i]-d[i] ) < eps && fabs( b[j]-a[j] ) < eps ) { p[i][j].x = a[j] ; p[i][j].y = c[i] ; } else if( fabs( c[i]-d[i] ) < eps && fabs( b[j]-a[j] ) > eps ) { p[i][j].x = c[i]*(b[j]-a[j]) + a[j] ; p[i][j].y = c[i] ; } else if( fabs( c[i]-d[i] ) > eps && fabs( b[j]-a[j] ) < eps ) { p[i][j].x = a[j] ; p[i][j].y = a[j]*(d[i]-c[i]) + c[i] ; } else { p[i][j] = f(p[0][j],p[n+1][j],p[i][0],p[i][n+1]) ; } } node s1 , e1 , s2 , e2 ; max1 = 0 ; for(i = 0 ; i <= n ; i++) for(j = 0 ; j <= n ; j++) { s1.x = p[i][j+1].x - p[i][j].x ; s1.y = p[i][j+1].y - p[i][j].y ; e1.x = p[i+1][j+1].x - p[i][j+1].x ; e1.y = p[i+1][j+1].y - p[i][j+1].y ; s2.x = p[i+1][j].x - p[i][j].x ; s2.y = p[i+1][j].y - p[i][j].y ; e2.x = p[i+1][j+1].x - p[i+1][j].x ; e2.y = p[i+1][j+1].y - p[i+1][j].y ; ans = ( fabs(s1.x*e1.y-s1.y*e1.x)/2.0 + fabs(s2.x*e2.y-s2.y*e2.x)/2.0 ) ; max1 = max(max1,ans) ; } printf("%.6lf\n", max1) ; } return 0; }
标签:
原文地址:http://blog.csdn.net/winddreams/article/details/43196949