• 叉积求面积 hdu2036(模版题)


    View Code
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    typedef struct node
    {
        double x;
        double y;
    }point;
    point p[105];
    
    point make_e(point x,point y)
    {
        point a;
        a.x = x.x - y.x;
        a.y = x.y - y.y;
        return a;
    }
    
    double cha(point x,point y)
    {
        return x.x*y.y - y.x*x.y;
    }
    
    int main()
    {
        int n,i,j;
        while(scanf("%d",&n)&&n)
        {
            for(i = 0;i < n;i++)
            scanf("%lf %lf",&p[i].x,&p[i].y);
    
            point e1,e2;
            e1 = make_e(p[0],p[1]);
            e2 = make_e(p[0],p[2]);
            double ans;
            ans = 0;
            ans += cha(e1,e2);
            for(i = 3;i < n;i++)
            {
                e1 = e2;
                e2 = make_e(p[0],p[i]);
                ans+= cha(e1,e2);
            }
            printf("%.1lf\n",ans/2);
        }
        return 0;
    }

    pc代码

    View Code
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    
    const int N=102;
    
    struct node
    {
        int x,y;
    }a,b,c,ss[N];
    int n;
    
    void gotp(node *p,int i,int j)
    {//向量赋值
        p->x=ss[j].x-ss[i].x;
        p->y=ss[j].y-ss[i].y;
    }
    double cm(node a,node b)
    {//叉积计算面积
        return (a.x*b.y-a.y*b.x);//可能有凹多边形,故未加fabs();
    }
    double calsum()
    {
        int i,j,k;
        double ans=0;
        gotp(&a,0,1);
        gotp(&b,0,2);
        ans+=cm(a,b);//第一个三角形面积的2倍被加入
        for(i=3;i<n;i++)
        {
            a=b;
            gotp(&b,0,i);
            ans+=cm(a,b);
        }
        return ans/2;
    }
    
    int main()
    {
        while(cin>>n,n)
        {
            memset(ss,0,sizeof(ss));
            for(int i=0;i<n;i++)
            cin>>ss[i].x>>ss[i].y;
            double ans=calsum();
            printf("%.1f\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    SQL Activity Monitor
    Oracle学习计划
    SQL Server 2008 R2下载地址
    聚集索引与非聚集索引的区别
    Android图片加载后变小
    工作手记之Cransoft(四)
    触发器
    Oracle数据库体系架构概要
    html5
    基础概念
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2640232.html
Copyright © 2020-2023  润新知