链接:点击打开链接
题意:求出能覆盖给出的所有点的最小正方形面积
代码:
#include <iostream> #include <cmath> #include <cstdio> using namespace std; double a[105],b[105]; int n; double cal(double degree){ double maxx,maxx1,minn,minn1; double x,y; int i; maxx=maxx1=-99999999;minn=minn1=99999999; for(i=0;i<n;i++){ x=a[i]*cos(degree)+b[i]*sin(degree); //推的公式x应该是加法,网上不少是减法 y=b[i]*cos(degree)-a[i]*sin(degree); maxx=max(maxx,x); maxx1=max(maxx1,y); minn=min(minn,x); minn1=min(minn1,y); } maxx-=minn;maxx1-=minn1; maxx=max(maxx,maxx1); maxx*=maxx; return maxx; } double threesearch(double l,double r){ double mid,midmid; int i; for(i=1;i<=100;i++){ mid=(r-l)/3+l; midmid=(r-l)/3*2+l; if(cal(mid)>=cal(midmid)) l=mid; else r=midmid; } return cal(l); } int main(){ int t,i; double temp; scanf("%d",&t); while(t--){ scanf("%d",&n); for(i=0;i<n;i++) scanf("%f%f",&a[i],&b[i]); temp=threesearch(0,acos(-1.0)); //旋转坐标轴,从0~pi printf("%.2f\n",temp); //提交时选C++,如果选G++,将.2lf改为.2f } return 0; }