在 C++ 中查詢等腰三角形的高和麵積
假設我們已知等腰三角形的邊長,我們的任務是求出它的面積和高。在這種型別的三角形中,兩條邊相等。假設三角形的邊長分別為 2、2 和 3,則高為 1.32,面積為 1.98。
高(h)=$$\sqrt{a^{2}-\frac{b^{2}}{2}}$$
面積(A)=$\frac{1}{2}*b*h$
示例
#include<iostream> #include<cmath> using namespace std; float getAltitude(float a, float b) { return sqrt(pow(a, 2) - (pow(b, 2) / 4)); } float getArea(float b, float h) { return (1 * b * h) / 2; } int main() { float a = 2, b = 3; cout << "Altitude: " << getAltitude(a, b) << ", Area: " << getArea(b, getAltitude(a, b)); }
輸出
Altitude: 1.32288, Area: 1.98431
廣告