X-man

导航

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\n",myfabs(ans/2));
        else
            printf("%I64d\n",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\n",myfabs(anx));
        else
            printf("%.1lf\n",fabs((double)ans/2));
    }

    return 0;
}
View Code

 

posted on 2013-08-04 17:32  雨钝风轻  阅读(270)  评论(0编辑  收藏  举报