uva1643 - Angle and Squares

出處https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4518

解題策略

外積求面積,最大面積為所有正方形對角線成一直線相連

待完成或參考程式碼

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

/*1643 - Angle and Squares,外積求面積, 最大面積為所有正方形對角線成一直線相連 */#include <cstdio>#include <cmath>using namespace std; int main(){ int n; double xa,ya,xb,yb,d,sumsqr,L,k1,k2,tmp,x1,x2,y1,y2; while(scanf("%d",&n)&&n){ sumsqr=L=0; scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb); for(int i=0;i<n;i++) { scanf("%lf",&d); L+=d;//正方形邊長和 sumsqr+=d*d/2;//正方形面積取一半 } k1=ya/xa; k2=yb/xb; if (k2<k1) { tmp=k1; k1=k2; k2=tmp; } x1=(k1+1)*L/(k2-k1); x2=(k2+1)*L/(k2-k1); y1=k1*x1; y2=k2*x2; double a=fabs(x1*y2-x2*y1)/2;//外積為平行四邊形面積,除以2變三角形 printf("%.3lf\n",a-sumsqr); } }