题目描述
给出三角形的三个边长为a,b,c,根据海伦公式来计算三角形的面积:
输入
输入数据只有一组,即为三角形的三个边长a,b,c (100≥a,b,c≥0)
输出
输出三角形的面积,两位小数。如果不是一个三角形,则输出错误提示信息:“Input error!”,注意输出没有引号。
样例输入
3 4 5
样例输出
6.00
//错误(一半):
#include<bits/stdc++.h>
using namespace std;
int main() {
double area,s,a,b,c;
cin>>a>>b>>c;
if(a+b<c or a+c<b or b+c<a)cout<<"Input error!";
else { //这里其他情况可能还存在三角形不成立的情况,如全为零.....
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<fixed<<setprecision(2)<<area<<endl;
}
return 0;
}
//正确代码
#include<bits/stdc++.h>
using namespace std;
int main() {
double area,s,a,b,c;
cin>>a>>b>>c;
if(a+b>c and a+c>b and b+c>a){
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<fixed<<setprecision(2)<<area<<endl;
}
else cout<<"Input error!"; //三角形不成立的情况有多种,用else排除的干净些
return 0;
}