找出三角形的內切圓半徑的 C++ 程式
本教程中,我們將討論一個程式,用於求出給定三角形的內切圓半徑。
為此,我們將獲得特定三角形各邊的資訊,我們的任務是找出該三角形內切圓的半徑。
求出內切圓半徑的公式為:
三角形面積/三角形半周長
示例
#include <bits/stdc++.h>
using namespace std;
//calculating the radius of incircle
float calc_radius(float a, float b, float c) {
if (a < 0 || b < 0 || c < 0)
return -1;
//half perimeter of triangle
float p = (a + b + c) / 2;
//area of triangle
float area = sqrt(p * (p - a) * (p - b) * (p - c));
float radius = area / p;
// Return the radius
return radius;
}
int main() {
float a = 4, b = 7, c = 9;
cout << calc_radius(a, b, c) << endl;
return 0;
}輸出
1.34164
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP