BUPT-homework/pset7/6-triangle.c

60 lines
1.1 KiB
C
Raw Normal View History

2021-11-04 20:25:05 +08:00
// #include <stdio.h>
//
// int judgeTriangle(int, int, int);
//
// int main() {
// int a, b, c;
//
// scanf("%d%d%d", &a, &b, &c);
//
// switch (judgeTriangle(a, b, c)) {
// case -1:
// printf("It is not a triangle.\n");
// break;
// case 0:
// printf("It is a scalenous triangle.\n");
// break;
// case 1:
// printf("It is a right-angled triangle.\n");
// break;
// case 2:
// printf("It is an isosceles triangle.\n");
// break;
// case 3:
// printf("It is a equilateral triangle.\n");
// break;
// }
//
// return 0;
// }
int judgeTriangle(int a, int b, int c) {
// First we bubble sort them.
int tmp;
if (a > b) {
tmp = a;
a = b;
b = tmp;
}
if (b > c) {
tmp = b;
b = c;
c = tmp;
}
// If this is a triangle
if (a + b <= c) {
return -1; // Not a triangle
} else if (b == c || a == b || a == c) {
if (a == b && b == c) {
return 3; // Equilateral
} else {
return 2; // Isosceles
}
} else if (a * a + b * b == c * c) {
return 1; // Right triangle
} else {
return 0;
}
}