1813 - 计算三角形面积0

通过次数

0

提交次数

0

时间限制 : 3 秒 内存限制 : 128 MB

编写程序,输入三角型的三条边长,求其面积。

 注意:对于不合理的边长输入要输出数据错误的提示信息

题目输入

第一行为整数n,n<100,代表数据组数

其后n行,每行3个整数,以空格分隔

题目输出

每行输出对应三角形的面积,如果三边长度无法构成三角形,输出No Answer。要求保留两位小数。

输入/输出样例

输入格式

2
3 4 5
1 10 20

输出格式

6.00
No Answer

C语言解答

#include <stdio.h>
#include <math.h>
int isTriangle(int a, int b, int c){
    if((a+b>c)&&(b+c>a)&&(a+c>b))
        return 1;
    return 0;
}
int main(){
    int a, b, c, n;
    double A, s;
    scanf("%d", &n);
    while(n--){
        scanf("%d %d %d", &a, &b, &c);
        if(isTriangle(a, b, c)){
            s = (a+b+c)/2.0;
            A = sqrt(s*(s-a)*(s-b)*(s-c));
            printf("%.2f\n", A);
        }else{
            printf("No Answer\n");
        }
    }
    return 0;
}

C++解答

#include<stdio.h>
#include<math.h>
int main()
{
  int count,a,b,c;
  float s,area;
  scanf("%d",&count);
  while(count--){
  scanf("%d%d%d",&a,&b,&c);
  if(a+b>c&&a+c>b&&b+c>a)
  {
  s =1.0/2*(a+b+c);
  area = sqrt(s*(s-a)*(s-b)*(s-c));
  printf("%.2f\n",area);
  }
  else 
  printf("No Answer\n");
  }
  return 0;
}