三角形

2018-11-11  本文已影响0人  有木

总时间限制: 1000ms 内存限制: 128000kB

描述

给出三个实数,表示三角形的三条边长,如果能构成三角形就输出三角形的面积,保留3位小数点输出,如果不能构成三角形就输出No

输入

三角形的三条边长

输出

三角形的面积

样例输入

2 3 4

样例输出

2.905

提示

已知三条边长求三角形面积用海伦公式:
a、b、c代表三角形的三条边长

p=(a+b+c)/2
s=sqrt(p(p-a)(p-b)*(p-c))

源代码

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;
class Triangle
{
    public:
        float a;
        float b;
        float c;
        
        Triangle(float, float, float);
        float getCircumference();
        float getArea();
};
Triangle::Triangle(float a, float b, float c)
{
    this->a = a;
    this->b = b;
    this->c = c;
}
float Triangle::getCircumference()
{
    float circumference = (a + b + c)/2;
    return circumference;
}
float Triangle::getArea()
{
    float area, circumference;
    circumference = getCircumference();
    // 海伦公式
    area = sqrt(circumference * (circumference-a) * (circumference-b) * (circumference-c));
    return area;
}

int main(int argc, char *argv[]) 
{
    float a, b, c;
    cin >> a >> b >> c;
    
    if(a+b>=c && a+c>=b && b+c>=a)
    {
        Triangle triangle = Triangle(a, b, c);
        // 设定float输出精度
        cout << setiosflags(ios::fixed) << setprecision(3)  << triangle.getArea();
    }
    else
    {
        cout << "No";
    }
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读