【深基3.习8】三角形分类

GitHub跳转原题关系图返回列表

先排序边长,再用平方关系判断角类型,并按顺序追加等腰、等边分类。

OJ: luogu

题目 ID: P5717

难度:入门

标签:python入门条件判断排序几何

日期: 2026-07-15 18:12

题意

输入三条线段长度,判断它们能否组成三角形;如果能,还要按顺序输出直角/锐角/钝角、等腰、等边等分类。

思路

先把三条边排序成 a <= b <= c。这样:

  • 只需要检查 a + b > c 就能判断是否能组成三角形;
  • 只需要比较 a*a + b*bc*c 就能判断角类型。

如果不能组成三角形,直接输出 Not triangle。否则把符合的分类字符串按题目要求的顺序加入 result 列表,最后用换行连接输出。

brute.py 不适合这题,因为它是规则分类题,排序后按公式判断就是完整解法。

Python 知识

  • sorted(...) 返回升序列表,方便统一把最长边放在 c
  • a, b, c = sides 是序列解包。
  • 用列表 result.append(...) 收集多行输出,比边判断边 print 更容易控制顺序。
  • "\n".join(result) 把多个答案行连接成最终输出。

对应的本地 Python 笔记:

  • /home/rainboy/mycode/hugo-blog/content/program_language/python/sorting_and_ordering.mdsorted 的用法。
  • /home/rainboy/mycode/hugo-blog/content/program_language/python/input_output_and_strings.md:多值输入和多行输出。
  • /home/rainboy/mycode/hugo-blog/content/program_language/python/oj_input_output_cheatsheet.md:输出多行答案。

代码

python
sides = sorted(map(int, input().split()))
a, b, c = sides

if a + b <= c:
    print("Not triangle", end="")
else:
    result = []
    left = a * a + b * b
    right = c * c

    if left == right:
        result.append("Right triangle")
    elif left > right:
        result.append("Acute triangle")
    else:
        result.append("Obtuse triangle")

    if a == b or b == c:
        result.append("Isosceles triangle")
    if a == c:
        result.append("Equilateral triangle")

    print("\n".join(result), end="")
cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-07-27 00:00
 * update_at: 2026-07-27 00:00
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a[3]; // 三条边长度
    cin >> a[0] >> a[1] >> a[2];
    // 排序使 a[0] <= a[1] <= a[2],方便判断三角形和角类型
    if (a[0] > a[1]) swap(a[0], a[1]);
    if (a[1] > a[2]) swap(a[1], a[2]);
    if (a[0] > a[1]) swap(a[0], a[1]);

    // 三角形两边之和必须大于第三边
    if (a[0] + a[1] <= a[2]) {
        cout << "Not triangle" << endl;
        return 0;
    }

    // 用平方关系判断角类型
    int left = a[0] * a[0] + a[1] * a[1];
    int right = a[2] * a[2];
    if (left == right) {
        cout << "Right triangle" << endl;
    } else if (left > right) {
        cout << "Acute triangle" << endl;
    } else {
        cout << "Obtuse triangle" << endl;
    }

    // 等腰:至少有两条边相等
    if (a[0] == a[1] || a[1] == a[2]) {
        cout << "Isosceles triangle" << endl;
    }
    // 等边:三条边都相等
    if (a[0] == a[2]) {
        cout << "Equilateral triangle" << endl;
    }
    return 0;
}

Pythonic 写法

match-case:

python
a, b, c = sorted(map(int, input().split()))

match True:
    case _ if a + b <= c:
        print("Not triangle", end="")
    case _:
        left = a * a + b * b
        right = c * c
        result = []

        match True:
            case _ if left == right:
                result.append("Right triangle")
            case _ if left > right:
                result.append("Acute triangle")
            case _:
                result.append("Obtuse triangle")

        match (a == b or b == c, a == c):
            case (True, True):
                result.append("Isosceles triangle")
                result.append("Equilateral triangle")
            case (True, False):
                result.append("Isosceles triangle")
            case _:
                pass

        print("\n".join(result), end="")

match-case 写法

三角形分类可用 match True + guard,以及等腰/等边的元组匹配:

python
a, b, c = sorted(map(int, input().split()))

match True:
    case _ if a + b <= c:
        print("Not triangle", end="")
    case _:
        left = a * a + b * b
        right = c * c
        result = []

        match True:
            case _ if left == right:
                result.append("Right triangle")
            case _ if left > right:
                result.append("Acute triangle")
            case _:
                result.append("Obtuse triangle")

        match (a == b or b == c, a == c):
            case (True, True):
                result.append("Isosceles triangle")
                result.append("Equilateral triangle")
            case (True, False):
                result.append("Isosceles triangle")
            case _:
                pass

        print("\n".join(result), end="")

复杂度

只排序 3 个数,可以看作时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

总结

几何分类题先把变量顺序整理好。排序后最长边固定为 c,三角形判断和角类型判断都会简单很多。