AStar百度之星2008初赛题目总结2.2:圆内五角星 (20分)

问题背景

如图,一个半径为1的圆周上有5个点。按角度制给出5个点的极角Ai (0<=Ai<360, i=1..5)。按下图的方法连成一个五角星, 计算圆被切割成的11个部分面积的方差。


具体地说, 假定11个区域的面积分别为S1,S2, ..., S11,那么面积的均值计算方法为:

M = (S1+S2+...+S11 ) / 11


面积的方差计算方法为:

D = ((S1-M)2 + (S2-M)2 + ... + (S11-M)2) / 11

输入格式

输入仅一行,包含5[0,359]内的互不相等的整数。

输出格式

输出仅一行,包含一个实数,即各部分面积的方差。输出保留小数点后4位。

样例输入

0 144 72 288 216

样例输出

0.0144

我对问题的分析

1、把极角排序(有利于后续计算),转化为直角坐标系坐标

2、求五角星内交点五个

3、求五个个星顶三角形面积

4、求出“弓形-三角形”面积,然后以五个小扇形为未知量解一个五元线性方程组,求出五个小扇形面积

5、由圆的面积减去求出的十个面积,得到重心的五边形面积

6、根据方差公式求出答案

【评价】这个方法基本属于按部就班的方法,因为没有发掘到圆内接五角星的特殊性质,所以并没有涉及到什么技巧。

我的代码

#include <stdio.h>
  
  
#include <stdlib.h>
  
  
#include <math.h>
  
  

#define PI 3.1415926535898

typedef struct POINT 
{
	double x;
	double y;
}Point,*lpPoint;//点坐标

struct COMB
{
	POINT p;
	POINT PL;//左交点
	POINT PR;//右交点
};//五端点的附带结构

COMB c[5];//结构数组
int arg[5];//角度
double areaG[5];//弓形
double area[11];//11个部分面积
//选择排序
void sort(int arr[], int n)
{
	int i, j, min, t;
	for (i = 0; i < n -1; i++)
	{
		min = i;
		for (j = i + 1; j < n; j++)
		{
			if (arr[min] > arr[j])
			{
				min = j;
			}
		}
		if (min != i)
		{
			t = arr[i];
			arr[i] = arr[min];
			arr[min] = t;
		}
	}	
}
//两线段交点
POINT GetCrossPoint(POINT p1, POINT p2, POINT q1, POINT q2)
{
	/*根据两点式化为标准式,进而求线性方程组*/
	POINT crossPoint;
	double tempLeft,tempRight;
	//求x坐标
	tempLeft = (q2.x - q1.x) * (p1.y - p2.y) - (p2.x - p1.x) * (q1.y - q2.y);
	tempRight = (p1.y - q1.y) * (p2.x - p1.x) * (q2.x - q1.x) + q1.x * (q2.y - q1.y) * (p2.x - p1.x) - p1.x * (p2.y - p1.y) * (q2.x - q1.x);
    	crossPoint.x =tempRight /tempLeft;
	//求y坐标
	tempLeft = (p1.x - p2.x) * (q2.y - q1.y) - (p2.y - p1.y) * (q1.x - q2.x);
	tempRight = p2.y * (p1.x - p2.x) * (q2.y - q1.y) + (q2.x- p2.x) * (q2.y - q1.y) * (p1.y - p2.y) - q2.y * (q1.x - q2.x) * (p2.y - p1.y);
	crossPoint.y =tempRight / tempLeft;
	return crossPoint;
}
//求所有交点
void cross()
{
	int i;
	for (i=0;i<5;i++)
	{
		c[i].PL = GetCrossPoint(c[i].p,c[(i+3)%5].p,c[(i+1)%5].p,c[(i+4)%5].p);
		c[(i+4)%5].PR = c[i].PL;
	}
}
//void Helen();
//double SideLength(POINT X,POINT Y);
//点到点的距离
double SideLength( POINT X,POINT Y )
{
	
	double r=sqrt(((X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y)));
	return r;
}
//海伦公式求三角形面积
void Helen()
{
	double a,b,d;	//三边长
	double p;		//平均值
	for (int i=0; i<5; i++)
	{
		a=SideLength(c[i].p,c[i].PL);
		b=SideLength(c[i].PR,c[i].PL);
		d=SideLength(c[i].p,c[i].PR);
		p=0.5*(a+b+d);
		area[i]=sqrt((p*(p-a)*(p-b)*(p-d)));
	}
}
//求五个弓形面积
void Arch()
{
	double x;
	double arc;
	double rui;
	for (int i=0;i<5;i++)
	{
		x=0.5*SideLength(c[(i+4)%5].p,c[(i+1)%5].p);
		rui = acos(x);
		arc=PI-2.0*acos(x);
		areaG[i]=0.5*arc-0.5*sin(arc);
	}
}

//解方程求弧边的五块小扇形面积
void Equation()
{
	double temp[5];
	//弓形减去三角(方程右边)
	for (int i=0; i<5; i++)
	{
		temp[i] = areaG[i] - area[i];
	}
	//求解
	area[5] = (temp[0]+temp[2]+temp[4]-temp[1]-temp[3])/2;
	area[6] = (temp[0]+temp[1]+temp[3]-temp[2]-temp[4])/2;
	area[7] = (temp[1]+temp[2]+temp[4]-temp[0]-temp[3])/2;
	area[8] = (temp[0]+temp[2]+temp[3]-temp[1]-temp[4])/2;
	area[9] = (temp[1]+temp[3]+temp[4]-temp[0]-temp[2])/2;
}

//求最后中间一块面积
void LastArea()
{
	double plus(0.0);
	for (int i=0;i<10;i++)
	{
		plus += area[i];
	}
	area[10] = PI - plus;
}

int main(void)
{
	int i;
	for (i=0;i<5;i++)
	{
		scanf("%d",&arg[i]);
	}
	sort(arg,5);//排序
	double d[5];
	for (i=0;i<5;i++)
	{
		d[i] = (double)(arg[i])*PI/180.0;
	}
	//点坐标
	for (i=0;i<5;i++)
	{
		c[i].p.x = cos(d[i]);
		c[i].p.y = sin(d[i]);
	}
	//求所有交点
	cross();
	//----------求面积--------------
	Helen();//五个三角形面积
	Arch();//弓形面积
	Equation();//解方程求弧边的五块小扇形面积
	LastArea();//求最后中间一块面积
	//对area[11]求方差
	double aver = PI/11.0;
	//printf("%.4f/n",aver);
	double result = 0.0;//加和
	for (i=0;i<11;i++)
	{
		result = result + (area[i]-aver)*(area[i]-aver);		
	}
	result = result/11.0;

	result = (float)((int)(result*10000+0.5))/10000.0;//四舍五入取四位
	printf("%.4f/n",result);
	return 0;
}
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值