14032: 【原4032】三角形判定
题目
题目描述
author: 程序设计思想与方法助教组李天格 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4032
问题描述
输入三个实数,判断这三个数能否构成三角形的三条边。
如果能构成三角形,当三条边构成直角三角形时, 输出1; 否则, 输出0。
如果不能构成三角形,输出-1。
输入输出描述
- 程序运行到输入时,不要显示输入提示信息.
- 输入为3个实数.
- 输出为相应的数字表示,结尾处不包含换行符、回车符.
运行示例
Sample Input1
1 2 9
Sample Output1
-1
Sample Input2
3.1 4.1 5.1
Sample Output2
0
Sample Input3
-3.1 4.1 5.1
Sample Output3
-1
Sample Input4
3 4 5
Sample Output4
1
victrid's solution
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double a, b, c, temp;
cin >> a >> b >> c;
//rank
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
//is triangle
if (c >= (a + b) || a <= 0) {
cout << "-1";
return 0;
}
//is RT
if ((c * c - a * a - b * b) <= 1e-6 && (c * c - a * a - b * b) >= -1e-6) {
cout << "1";
} else {
cout << "0";
}
return 0;
}