実験3-8三角形の面積と円周を出力する(15点)

この質問では、入力三角形の3つの辺に基づいa、b、cて面積と周囲長計算して出力するプログラムを作成する必要があります注:三角形では、任意の2つの辺の合計が3番目の辺よりも大きくなります。三角形の面積を計算する式:
area = s(s − a)(s − b)(s − c)area = \ sqrt {s} \ sqrt {(s−a)} \ sqrt { (s-b)} \ sqrt {(s-c)}a r e a=s s a s b s c
どこs=(a+b+c)/2

入力フォーマット:

入力は、三角形の3つの辺を表す3つの正の整数a、b、cです。

出力フォーマット:

入力側が三角形を一列に形成できる場合、

area = 面积; perimeter = 周长

出力形式は、小数点以下2桁を保持することです。それ以外の場合は、出力

These sides do not correspond to a valid triangle

入力例1:

5 5 3

出力サンプル1:

area = 7.15; perimeter = 13.00

入力例2:

1 4 1

出力サンプル2:

These sides do not correspond to a valid triangle

コード:

# include <stdio.h>
# include <stdlib.h>
# include <math.h>

int main() {
    
    
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    double area,perimeter,s;
    // 能构成三角形的条件
    if (a + b > c && a + c > b && b + c > a) {
    
    
        s = (a + b + c) / 2.0;
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        perimeter = 2 * s;
        printf("area = %.2lf; perimeter = %.2lf",area,perimeter);
    }else {
    
    
        printf("These sides do not correspond to a valid triangle");
    }
    return 0;
}

スクリーンショットを送信:

ここに画像の説明を挿入

問題解決のアイデア:

この質問の難しさは、三角形の状態の判断にあります。

  • 任意の2つの辺の合計が3番目の辺よりも大きい

おすすめ

転載: blog.csdn.net/weixin_43862765/article/details/114454636