zoj1032 Area 2

  1. //zoj1032 Area 2

  2. //Accepted 1032 C++ 00:00.00 392K 

  3. #include <stdio.h>

  4. #define MAXN 100

  5.  

  6. struct point{ double x,y;};

  7.  

  8. double xmult(double x1,double y1,double x2,double y2){ return (x1*y2-y1*x2);}

  9. int gcd(int a,int b) { return b?gcd(b,a%b):a;}

  10. int abs(int x) { return x>0?x:(0-x);}

  11.  

  12. double area_polygon(int n, point *p)

  13. {

  14.        int i;

  15.        double ret=0;

  16.        for (i=0; i<n; ++i)

  17.               ret += xmult(p[i].x,p[i].y,p[i+1].x,p[i+1].y);

  18.        ret += xmult(p[n-1].x,p[n-1].y,p[0].x,p[0].y);

  19.        return ret/2;

  20. }

  21.  

  22. void solve()

  23. {

  24.        int i,n;

  25.        point p[MAXN+1];

  26.        int c,e;

  27.        double a;

  28.        int dx,dy;

  29.        scanf ("%d",&n);

  30.        p[0].x = p[0].y = 0;

  31.        e = 0;

  32.        for (i=0; i<n; ++i){

  33.               scanf ("%d %d",&dx,&dy);

  34.               e += gcd(abs(dx),abs(dy));

  35.               p[i+1].x = p[i].x+dx;

  36.               p[i+1].y = p[i].y+dy;

  37.        }

  38.        a = area_polygon(n+1,p);

  39.        c = (int)(a+1-0.5*(double)e);

  40.        printf ("%d %d %.1lf/n/n",c,e,a);

  41. }

  42.  

  43. int main()

  44. {

  45. #ifdef ONLINE_JUDGE

  46. #else

  47.        freopen("1032.txt","r",stdin);

  48. #endif

  49.        int i,t;

  50.        while (scanf ("%d",&t) != EOF)

  51.               for (i=1; i<=t; ++i){

  52.                      printf ("Scenario #%d:/n",i);

  53.                      solve();

  54.               }

  55. #ifdef ONLINE_JUDGE

  56. #else

  57.        fclose(stdin);

  58. #endif

  59.        return 0;

  60. }
阅读更多
文章标签: struct c
个人分类: ZOJ
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭