• poj 1654 Area


    #include<stdio.h>
    #include<string.h>
    #define Max 1000100
    char s[Max];
    struct Point
    {
        int x,y;
    } p[Max];
    long long myfabs(long long x)
    {
        if(x<0)x=-x;
        return x;
    }
    int xmult(Point p1,Point p2,Point p0)
    {
        return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    }
    long long cal(Point *p,int n)
    {
        int i,j;
        long long ans=0;
        for(i=1; i<n-1; i++)
        {
            ans+=xmult(p[i],p[i+1],p[0]);
        }
        return ans;
    }
    int x,y;
    void change(char c)
    {
        if(c=='1')
        {
            x-=1;
            y-=1;
        }
        else if(c=='2')y-=1;
        else if(c=='3')
        {
            x+=1;
            y-=1;
        }
        else if(c=='4')x-=1;
        else if(c=='6')x+=1;
        else if(c=='7')
        {
            x-=1;
            y+=1;
        }
        else if(c=='8')y+=1;
        else if(c=='9')
        {
            x+=1;
            y+=1;
        }
    }
    int main()
    {
        int i,n;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",s);
            x=y=0;
            p[0].x=p[0].y=0;
            int sl=strlen(s);
            for(i=1; i<sl; i++)
            {
                change(s[i-1]);
                p[i].x=x;p[i].y=y;
            }
            //memset(s,0,sizeof(s));
            long long ans=cal(p,sl);
           // printf("%d#",ans);
            if(ans&1)
                printf("%I64d.5
    ",myfabs(ans/2));
            else
                printf("%I64d
    ",myfabs(ans/2));
        }
    
        return 0;
    }

    用double精度不够

    用int范围不够;

    第二种解决方法(处理机器误差):

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #define Max 1000100
    #define eps 1e-12
    char s[Max];
    struct Point
    {
        int x,y;
    } p[Max];
    long long myfabs(long long x)
    {
        if(x<0)x=-x;
        return x;
    }
    int xmult(Point p1,Point p2,Point p0)
    {
        return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
    }
    long long cal(Point *p,int n)
    {
        int i,j;
        long long ans=0;
        for(i=1; i<n-1; i++)
        {
            ans+=xmult(p[i],p[i+1],p[0]);
        }
        return ans;
    }
    int x,y;
    void change(char c)
    {
        if(c=='1')
        {
            x-=1;
            y-=1;
        }
        else if(c=='2')y-=1;
        else if(c=='3')
        {
            x+=1;
            y-=1;
        }
        else if(c=='4')x-=1;
        else if(c=='6')x+=1;
        else if(c=='7')
        {
            x-=1;
            y+=1;
        }
        else if(c=='8')y+=1;
        else if(c=='9')
        {
            x+=1;
            y+=1;
        }
    }
    int main()
    {
        int i,n;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",s);
            x=y=0;
            p[0].x=p[0].y=0;
            int sl=strlen(s);
            for(i=1; i<sl; i++)
            {
                change(s[i-1]);
                p[i].x=x;p[i].y=y;
            }
            //memset(s,0,sizeof(s));
            long long ans=cal(p,sl);
           // printf("%d#",ans);
            //if(ans&1)
            double anx=(double)ans/2;
            if(fabs(anx-(long long)anx)<eps)
                printf("%I64d
    ",myfabs(anx));
            else
                printf("%.1lf
    ",fabs((double)ans/2));
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    Wireshark for Ethical Hackers 8
    PowerShell Deep Dive
    Blind signature
    Aggregate Signatures
    group signature
    PBC库的安装与使用_Windows_Ubuntu
    Attributebased Signature
    IdentityBased Signature and Certificateless Signature
    ServerAided Verification Signature
    安全多方计算背景知识
  • 原文地址:https://www.cnblogs.com/XDJjy/p/3236593.html
Copyright © 2020-2023  润新知