問題背景
如圖,一個(gè)半徑為1的圓周上有5個(gè)點(diǎn)。按角度制給出5個(gè)點(diǎn)的極角Ai (0<=Ai<360, i=1..5)。按下圖的方法連成一個(gè)五角星, 計(jì)算圓被切割成的11個(gè)部分面積的方差。
具體地說, 假定11個(gè)區(qū)域的面積分別為S1,S2, ..., S11,那么面積的均值計(jì)算方法為:
M = (S1+S2+...+S11 ) / 11
面積的方差計(jì)算方法為:
D = ((S1-M)2 + (S2-M)2 + ... + (S11-M)2) / 11
輸入格式
輸入僅一行,包含5個(gè)[0,359]內(nèi)的互不相等的整數(shù)。
輸出格式
輸出僅一行,包含一個(gè)實(shí)數(shù),即各部分面積的方差。輸出保留小數(shù)點(diǎn)后4位。
樣例輸入
0 144 72 288 216
樣例輸出
0.0144
我對(duì)問題的分析:
1、把極角排序(有利于后續(xù)計(jì)算),轉(zhuǎn)化為直角坐標(biāo)系坐標(biāo)
2、求五角星內(nèi)交點(diǎn)五個(gè)
3、求五個(gè)個(gè)星頂三角形面積
4、求出“弓形-三角形”面積,然后以五個(gè)小扇形為未知量解一個(gè)五元線性方程組,求出五個(gè)小扇形面積
5、由圓的面積減去求出的十個(gè)面積,得到重心的五邊形面積
6、根據(jù)方差公式求出答案
【評(píng)價(jià)】這個(gè)方法基本屬于按部就班的方法,因?yàn)闆]有發(fā)掘到圓內(nèi)接五角星的特殊性質(zhì),所以并沒有涉及到什么技巧。
我的代碼
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926535898
typedef struct POINT
{
double x;
double y;
}Point,*lpPoint;//點(diǎn)坐標(biāo)
struct COMB
{
POINT p;
POINT PL;//左交點(diǎn)
POINT PR;//右交點(diǎn)
};//五端點(diǎn)的附帶結(jié)構(gòu)
COMB c[5];//結(jié)構(gòu)數(shù)組
int arg[5];//角度
double areaG[5];//弓形
double area[11];//11個(gè)部分面積
//選擇排序
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;
}
}
}
//兩線段交點(diǎn)
POINT GetCrossPoint(POINT p1, POINT p2, POINT q1, POINT q2)
{
/*根據(jù)兩點(diǎn)式化為標(biāo)準(zhǔn)式,進(jìn)而求線性方程組*/
POINT crossPoint;
double tempLeft,tempRight;
//求x坐標(biāo)
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坐標(biāo)
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;
}
//求所有交點(diǎn)
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);
//點(diǎn)到點(diǎn)的距離
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)));
}
}
//求五個(gè)弓形面積
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;
}
//點(diǎn)坐標(biāo)
for (i=0;i<5;i++)
{
c[i].p.x = cos(d[i]);
c[i].p.y = sin(d[i]);
}
//求所有交點(diǎn)
cross();
//----------求面積--------------
Helen();//五個(gè)三角形面積
Arch();//弓形面積
Equation();//解方程求弧邊的五塊小扇形面積
LastArea();//求最后中間一塊面積
//對(duì)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;
}