《信息学奥赛一本通》题库 1034 计算三角形面积——基础

开始复健。

用了不方便的算面积的方法,讨论了好多,还调了一个多小时- -

发现 double 和 longdouble 的四舍五入是随机的,有时候舍5有时候入5。

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
long double x[3],y[3],x2,y2,tx,ty,dc;
double ans;
int bj=-1;
bool fx;
int main()
{
  for(int i=0;i<3;i++)
    scanf("%Lf%Lf",&x[i],&y[i]);
  for(int i=0;i<3;i++)
    {
      int d=i+1; if(d==3)d=0;
      /*printf("i=%d d=%d (%.1Lf,%.1Lf) (%.1Lf,%.1Lf)\n",
    i,d,x[i],y[i],x[d],y[d]);*/
      x2=x[i]-x[d]; if(x2<0)x2=-x2;
      y2=y[i]-y[d]; if(y2<0)y2=-y2;
      dc+=x2*y2;
      /*printf("  x1=%.1Lf y1=%.1Lf dc=%.1Lf\n",x2,y2,dc);
    printf("  tx=%.1Lf ty=%.1Lf\n",tx,ty);*/
      if(i==0)tx=x2,ty=y2,bj=2,fx=1;
      else
    {
      if(x2>tx&&y2>ty)
        {
          bj=(i+2)%3; fx=1; tx=x2; ty=y2;
        }
      if(x2>tx)tx=x2,fx=0; if(y2>ty)ty=y2,fx=0;
      //printf("i=%d d=%d fx=%d\n",i,d,fx);
    }
    }
  dc/=2;
  //printf("bj=%d fx=%d\n",bj,fx);
  if(bj!=-1&&fx)
    {
      double tmp=0;
      /*for(int i=0;i<3;i++)
    {
      printf("x[%d]=%.2Lf y[%d]=%.2Lf\n",
         i,x[i],i,y[i]);
    }
    printf("bj=%d\n",bj);*/
      if(bj!=1)
    {
      swap(x[bj],x[1]); swap(y[bj],y[1]);
    }
      /*for(int i=0;i<3;i++)
    {
      printf("x[%d]=%.2Lf y[%d]=%.2Lf\n",
         i,x[i],i,y[i]);
         }*/
      if((x[0]-x[2])*(y[0]-y[2])>=0)
    {
      if((y[0]-y[1])/(x[0]-x[1])<(y[0]-y[2])/(x[0]-x[2]))
        tmp=abs((x[1]-x[2])*(y[0]-y[1]));
      else tmp=abs((x[0]-x[1])*(y[1]-y[2]));
    }
      else
    {
      if((y[0]-y[1])/(x[0]-x[1])<(y[0]-y[2])/(x[0]-x[2]))
        tmp=abs((x[0]-x[1])*(y[1]-y[2]));
      else tmp=abs((x[1]-x[2])*(y[0]-y[1]));
    }
      dc+=tmp;
    }
  //printf("tx=%.1Lf ty=%.1Lf\n",tx,ty);
  ans=tx*ty-dc;
  /*for(int i=0;i<3;i++)
    {
      int d=i+1; if(d==3)d=0;
      long double px=x[i]-x[d]; if(px<0)px=-px;
      long double py=y[i]-y[d]; if(py<0)py=-py;
      if(tx<px||ty<py)
    {
      printf("NO\n");
      printf("x1=%Lf y1=%Lf px=%Lf py=%Lf\n",
         x1,y1,px,py);
      return 0;
    }
    }
    printf("YES\n");*/
  int d=floor(ans*1000); d%=10;
  if(d==5)ans+=0.001;
  printf("%.2lf\n",ans);
  return 0;
}
View Code
#include<cstdio>
#include<ctime>
#include<algorithm>
using namespace std;
int a,b; double c,d;
int main()
{
  srand(time(0));
  for(int i=0;i<3;i++)
    {
      a=rand()%100; b=rand()%100;
      c=(double)a/10; d=(double)b/10;
      printf("%lf %lf",c,d);
      if(i!=2)putchar(' '); else putchar('\n');
    }
  return 0;
}
mk
#include<cstdio>
#include<algorithm>
using namespace std;
int cnt;
int main()
{
  while(1)
    {
      cnt++; printf("cnt=%d\n",cnt);
      system("./mk > 1.in");
      system("./a < 1.in > zj.out");
      system("./TJ < 1.in > TJ.out");
      if(system("diff zj.out TJ.out"))break;
      if(cnt==1000)break;
    }
  return 0;
}
dp

 

posted on 2020-07-16 23:54  Narh  阅读(816)  评论(0编辑  收藏  举报

导航