主要思路:
我先用三个 If语句,从大到小排列abc的值
int temp;
if (a>b)
{
temp = a;
a = b;
b = temp;
}
if (a>c)
{
temp = a;
a = c;
c = temp;
}
if (b>c)
{
temp = b;
b = c;
c = temp;
}
然后按照题目给出的要求来建立运算式,这里要用到一个库函数pow,它存在于math库中
if (a+b>c && a+c>b && b+c>a)
{
if ( pow(a,2) + pow(b,2) == pow(c,2) )
{
printf("Right triangle\n");
}
if ( pow(a,2) + pow(b,2) > pow(c,2) )
{
printf("Acute triangle\n");
}
if ( pow(a,2) + pow(b,2) < pow(c,2) )
{
printf("Obtuse triangle\n");
}
if (a==b || a==c || b==c)
{
printf("Isosceles triangle\n");
}
if (a==b && b==c)//注意这里不能直接写a==c==b
{
printf("Equilateral triangle\n");
}
}
else printf("Not triangle");
完整代码:
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int temp;
if (a>b)
{
temp = a;
a = b;
b = temp;
}
if (a>c)
{
temp = a;
a = c;
c = temp;
}
if (b>c)
{
temp = b;
b = c;
c = temp;
}
if (a+b>c && a+c>b && b+c>a)
{
if ( pow(a,2) + pow(b,2) == pow(c,2) )
{
printf("Right triangle\n");
}
if ( pow(a,2) + pow(b,2) > pow(c,2) )
{
printf("Acute triangle\n");
}
if ( pow(a,2) + pow(b,2) < pow(c,2) )
{
printf("Obtuse triangle\n");
}
if (a==b || a==c || b==c)
{
printf("Isosceles triangle\n");
}
if (a==b && b==c)
{
printf("Equilateral triangle\n");
}
}
else printf("Not triangle");
return 0;
}