题目-求三角形面积

题目-求三角形面积

输入三角形的三边长,求其面积。

输入格式:

在一行中输入能够构成三角形的3个实数,3个数之间用逗号间隔。

输出格式:

对每一组输入,在一行中输出面积值,结果保留两位小数,没有附加字符。

输入样例:

3 3 3

输出样例:

3.90

分析过程

已知三角形三边求面积可用海伦公式

用 sqart() 函数求平方根,加头文件 #include<cmath>

保留两位小数,加头文件 #include<iomanip>,两种写法:

1 //(1)
2 cout.setf(ios::fixed, ios::floatfield);
3 cout << setprecision(2) << S << endl;
4 
5 //(2),仅对随其后的一个输出有效
6 cout << fixed << setprecision(2) << S << endl;

代码

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 int main()
 6 {
 7   float a,b,c,p;
 8   char d1,d2;
 9   cin>>a>>d1>>b>>d2>>c;
10     double S;
11     p=(a+b+c)/2.0;
12     S=sqrt(p*(p-a)*(p-b)*(p-c));
13     cout<<fixed<<setprecision(2)<<S<<endl;
14   return 0;
15 }
原文地址:https://www.cnblogs.com/yuanchuying/p/14750621.html