【HDUoj 1071】The area

本文介绍了一种使用数学公式计算由三个顶点定义的三角形面积的方法,并通过C++代码实现了该算法。具体步骤包括确定直线方程的系数,然后计算所围成区域的面积,并进行四舍五入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The area

思路

  • 利用数学知识计算出面积
  • 加0.5实现四舍五入

代码

#include <iostream>
#include <string.h> 
using namespace std;
#define LOCAL 0

//three points 
double x1,x2,x3,y1,y2,y3;   
//the coefficients of the two functions
double a,b,c,k,t;           
double s;   //area

int main(){   
#if LOCAL
    freopen ("datain.txt","r",stdin);
    freopen ("dataout.txt","w",stdout);
#endif

    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> x1 >> y1;
        cin >> x2 >> y2;
        cin >> x3 >> y3;

        a = (((y2-y1) / (x2-x1)) - ((y3-y2) / (x3-x2))) / (x1-x3);
        b = ((y2-y1) / (x2-x1)) - (a * (x1+x2));
        c = y3 - (a * x3 * x3 + b * x3);
        k = (y3 - y2) / (x3 - x2);
        t = y2 - k * x2;
        s = ((a / 3)*x3*x3*x3 + ((b-k)/2)*x3*x3 + (c-t)*x3)
                - ((a / 3)*x2*x2*x2 + ((b-k)/2)*x2*x2 + (c-t)*x2);
        //round-off 
        int m = s * 100 + 0.5;
        s = m / 100.0;
        printf("%0.2f\n",s);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值