谁帮写下C程序

来源:百度知道 编辑:UC知道 时间:2024/06/03 23:50:36
谁帮忙作个简单题..已知三角形的三边长分别为3,4,5, 试用海伦公式编程求其面积。海伦公式为:S△=“根号”下面 s(s-a)(s-b)(s-c) .其中s=1/2(a+b+c)写的有点乱....谁帮写下程序,我初学者""
main(){int a=3,b=4,c=5,s;float S;s=(a+b+c)/2;S=sqrt(s*(s-a)*(s-b)*(s-c));printf("%d",S);} 请帮改下

#include<stdio.h>
#include<math.h>
void main()
{
int a=3,b=4,c=5,s,S;
s=(a+b+c)/2;
S=sqrt(s*(s-a)*(s-b)*(s-c));
printf("S=%d\n", S);
}
运行过啦
S=6

帮你运行啦结果为零,不过我用手算拉一下结果为6有看拉一下你的代码问题出在printf("%d",S);把%d改成%f,这下好啦兄弟该给分啦吧

#include <stdio.h>
#include <math.h>
void main()
{
int a = 3, b = 4, c = 5;
float S,s;
s = (a + b + c) / 2.0;
S = sqrt(s *(s - a)*(s - b)*(s - c));
printf("%.2f\n", S);
}

main(){
int a=3,b=4,c=5;
float s,S;
s=(a+b+c)/2;//避免损失精度
S=Math.sqrt(s*(s-a)*(s-b)*(s-c));//调用库方法
printf("%d",S);
}