Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

又臭又长的代码贴一个吧

Posted by StepByStepCnmLife at 2015-05-12 21:58:28 on Problem 1408
#include <stdio.h>
#define MAXSIZE 50
typedef struct Point{
    double x;
    double y;
}Point;
double a[4][MAXSIZE];
double s[MAXSIZE][MAXSIZE];
Point  v[MAXSIZE][MAXSIZE];//实际数组长度为 n+2
int   n;
Point intersection(Point u1,Point u2,Point v1,Point v2)//求两直线的交点
{
	Point ret=u1;
	double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
	/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
	ret.x+=(u2.x-u1.x)*t;
	ret.y+=(u2.y-u1.y)*t;
	return ret;
}
double cross(const Point A,const Point B, const Point C)
{
    return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}
void cal_intersection()
{
    int i,j;
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
        v[i][j]=intersection(v[0][j],v[n+1][j],v[i][0],v[i][n+1]);
}
double cal_area()//计算每一个以a[i][j]为顶点(向右下方辐射)的面积,
{
   int i,j;
   Point origin;
   double area,maxarea=0;
   origin.x=0,origin.y=0;
   for(i=0;i<=n;i++)
   for(j=1;j<=(n+1);j++)
   {
        area =cross(origin,v[i][j-1],v[i+1][j-1]);
        area+=cross(origin,v[i+1][j-1],v[i+1][j]);
        area+=cross(origin,v[i+1][j],v[i][j]);
        area+=cross(origin,v[i][j],v[i][j-1]);
        s[i][j]=area/2;
        //printf("%.6lf\n",area/2);
        maxarea=maxarea>s[i][j]?maxarea:s[i][j];
   }
   return maxarea;
}
int main()
{
    int i,j;
    double maxarea;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        for(i=0;i<4;i++)
        for(j=1;j<=n;j++)
            scanf("%lf",&a[i][j]);
        /**********对四条边上的点进行赋值**/
        for(i=1;i<=n;i++)
        {v[i][0].x=a[0][i]   ,v[i][0].y=0;
         v[i][n+1].x=a[1][i] ,v[i][n+1].y=1;
         v[0][i].y=a[2][i]   ,v[0][i].x=0;
         v[n+1][i].y=a[3][i] ,v[n+1][i].x=1;
        }
        v[0][0].x=0  ,v[0][0].y=0;
        v[n+1][0].x=1,v[n+1][0].y=0;
        v[0][n+1].x=0,v[0][n+1].y=1;
        v[n+1][n+1].x=1,v[n+1][n+1].y=1;
        /**********计算中间直线交点********/
        cal_intersection();//pass
        /*********求每一个四边形的面积*****/
        maxarea=cal_area();
        /*********找到最大的面积***********/
        printf("%.6lf\n",maxarea);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator