A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Mike001 中级黑马   /  2015-4-19 16:30  /  793 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

#include<stdio.h>
#include<math.h>
void main()
{
int a,p,c;
float area;

printf("Input a side of triangle: ");
scanf("%d",&a);
p=(3*a)/2;
c=3*a;
area=sqrt(p*(p-a)^3);
printf("The area of triangle is %.2f, the circle of triangle is %d.",area,c);
}根据“海伦-秦九韶”公式,area=√p(p-a)(p-b)(p-c),其中p=(a+b+c)/2计算三角形的面积和周长,结果周长是错误的,面积是错误的,求各位解释下

1 个回复

倒序浏览
恩,大概理解了一下你的题目,周长的结果应该是对的(但周长只能是整数),面积那里计算错了
  1. area=sqrt(p*(p-a)^3);
复制代码


^ 这个符号是位运算的异或,不是你以为的3次方,改成下面的就ok
  1. area=sqrt(p*pow((p-a),3));
复制代码


把你的完整代码给改了一下,你可以看看,不知道我的理解对不对

  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5.     int a,p,c;
  6.     float area;
  7.    
  8.     printf("Input a side of triangle: ");
  9.     scanf("%d",&a);
  10.     c=3*a;
  11.     p=c/2;
  12.    
  13.     area=sqrt(p*pow((p-a),3));
  14.     printf("The area of triangle is %.2f, the circle of triangle is %d.",area,c);

  15.     return 0;

  16. }
复制代码

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马