蓝桥杯-三角形 已知三点求三角形周长、面积、外心、重心

题目大意:已知三点求三角形周长、面积、外心、重心,将结果保留两位小数

题目分析:周长很好求,只要将三条线段的长度相加即可。面积的求法有专门的公式,比较快捷。

外心是三角形三条线段上中垂线交点,求法是求出两条中垂线,然后求它们的交点即可。

重心是三条中线的交点,只要求出三点的均值即可。((x1+x2+x3)/3,(y1+y2+y3)/3)


代码展示:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <math.h>
using namespace std;

struct Point{
    int x,y;
};

double len(Point p1,Point p2){
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}

int main(){
    Point p[3];
    for(int i=0;i<3;i++){
        cin>>p[i].x>>p[i].y;
    }
    double perimeter = 0;
    for(int i=0;i<3;i++){
        perimeter += len(p[i],p[(i+1)%3]);
    }
    cout<<fixed<<setprecision(2)<<perimeter<<endl;
    double a = len(p[0],p[1]);
    double b = len(p[1],p[2]);
    double c = len(p[2],p[0]);
    double s = 0.5*(a+b+c);
    double area = sqrt(s*(s-a)*(s-b)*(s-c));
    cout<<fixed<<setprecision(2)<<area<<endl;
    double a1 = p[1].x - p[0].x;
    double b1 = p[1].y - p[0].y;
    double c1 = (p[0].y * p[0].y - p[1].y * p[1].y + p[0].x * p[0].x - p[1].x * p[1].x) * 0.5000 ;
    double a2 = p[1].x - p[2].x ;
    double b2 = p[1].y - p[2].y ;
    double c2 = (p[2].y * p[2].y - p[1].y * p[1].y + p[2].x * p[2].x - p[1].x * p[1].x) * 0.5000 ;
    double X = (c1 * b2 - b1 * c2) / (b1 * a2 - a1 * b2) ;
    double Y = (a1 * c2 - c1 * a2) / (b1 * a2 - a1 * b2) ;
    cout<<fixed<<setprecision(2)<<X<<" ";
    cout<<fixed<<setprecision(2)<<Y<<endl;
    double weightx = (p[0].x+p[1].x+p[2].x)*1.0/3;
    double weighty = (p[0].y+p[1].y+p[2].y)*1.0/3;
    cout<<fixed<<setprecision(2)<<weightx<<" ";
    cout<<fixed<<setprecision(2)<<weighty<<endl;
    return 0;
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值