解題思路
排序成 a≤b≤c;正邊長下,只需檢查 a+b>c 就能判定非退化三角形。
能沿一條線切成兩個全等直角三角形時,兩半共用切線,原三角形必須有一對等長邊;反之等腰三角形由頂角作高,可得到兩個全等直角三角形。
所以合法後檢查 a=b 或 b=c。正三角形也符合。
每組 O(1) 時間與額外空間。
Step-by-step animation
三角不等式與等腰判斷|輸入 5,3,4,排序為 3,4,5。
步驟 1 / 3
排序
輸入 5,3,4,排序為 3,4,5。
3
4
5
- 兩短邊和
- 7
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
array<int, 3> a;
for (int &x : a)
cin >> x;
sort(a.begin(), a.end());
if (a[0] + a[1] <= a[2])
cout << "0\n";
else
cout << "1 " << (a[0] == a[1] || a[1] == a[2]) << "\n";
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。