三角形判断
时间限制(普通/Java) :
1000 MS/ 3000 MS 运行内存限制 : 65536 KByte
总提交 : 2731 测试通过 : 453
总提交 : 2731 测试通过 : 453
比赛描述
给定三条边的长度,判断能否组成三角形,如果可以,判断三角形的形状。
输入
一组数据,每行三个实数,在(0,10]之间,精确到小数点后第四位。最后以0 0 0表示结束。
输出
根据每行的数据判断,如果不能组成三角形,则输出“Not a triangle”;如果是“等腰三角形”,则输出“Isosceles triangle”;如果是“直角三角形”,则输出“Right triangle”;如果是“等腰直角三角形”,则输出“Isosceles right triangle”;如果是“等边三角形”,则输出“Equilateral triangle”;否则,输出“General triangle”。最后输出一行“End”。
样例输入
1.4142 1.4142 2
1.0000 4.0000 5.0000
0 0 0
样例输出
Isosceles right triangle
Not a triangle
End
同样是学c语言时候做的题~ 一年前的我还不知道代码规范是什么。。
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int Isosceles(double a,double b,double c);
int Right(double a,double b,double c);
int istriangle(double a,double b,double c)
{
if(a<1e-3) return 0;
if(a+b-c>1e-3)
return 1;
return 0;
}
int Equilateral(double a,double b,double c)
{
if(fabs(a-c)<1e-3)
return 1;
return 0;
}
int IsRight(double a,double b,double c)
{
double aa=pow(a*1.0,2),bb=pow(b*1.0,2),cc=pow(c*1.0,2);
if(fabs(aa+bb-cc)<1e-3&&(fabs(a-b)<1e-3))
return 1;
return 0;
}
int Right(double a,double b,double c)
{
if(!(fabs(a-b)<1e-3))
{
double aa=pow(a*1.0,2),bb=pow(b*1.0,2),cc=pow(c*1.0,2);
if(fabs(aa+bb-cc)<1e-3)
return 1;
}
return 0;
}
int Isosceles(double a,double b,double c)
{ if(fabs(a-b)<1e-3||fabs(c-b)<1e-3) return 1;
return 0;
}
int main()
{
double temp[5];
int sum;
while(1)
{
scanf("%lf%lf%lf",&temp[0],&temp[1],&temp[2]);
if(temp[0]==0) break;
sum=0;
//sort
sort(temp,temp+3);
if(istriangle(temp[0],temp[1],temp[2])==0)
printf("Not a triangle\n");
else
{
if(Equilateral(temp[0],temp[1],temp[2])==1)
{
sum++;
printf("Equilateral triangle\n");
}
else
{
if(IsRight(temp[0],temp[1],temp[2])==1)
{
sum++;
printf("Isosceles right triangle\n");
}
else
{
if(Isosceles(temp[0],temp[1],temp[2])==1)
{
sum++;
printf("Isosceles triangle\n");
}
else
{
if(Right(temp[0],temp[1],temp[2])==1)
{
sum++;
printf("Right triangle\n");
}
}
}
}
if(sum==0)
{
printf("General triangle\n");
}
}
}
printf("End");
return 0;
}