1 条题解
-
0
练54.2 小科的三角形
解题思路
先排序为a≤b≤c。若a²+b²=c²,输出c²;否则任取两边作直角边的斜边平方为两平方和,最大由b、c得到。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
不是直角时取最大的两边,不是最小两边。
复杂度
时间O(1),空间O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long a[3]; for (auto &x : a) cin >> x; sort(a, a + 3); if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2]) cout << a[2] * a[2]; else cout << a[1] * a[1] + a[2] * a[2]; cout << "\n"; return 0; }
- 1