返回介绍

solution / 3000-3099 / 3053.Classifying Triangles by Lengths / README

发布于 2024-06-17 01:02:57 字数 2957 浏览 0 评论 0 收藏 0

3053. Classifying Triangles by Lengths

English Version

题目描述

Table: Triangles

+-------------+------+ 
| Column Name | Type | 
+-------------+------+ 
| A       | int  | 
| B       | int  |
| C       | int  |
+-------------+------+
(A, B, C) is the primary key for this table.
Each row include the lengths of each of a triangle's three sides.

Write a query to find the type of triangle. Output one of the following for each row:

  • Equilateral: It's a triangle with 3 sides of equal length.
  • Isosceles: It's a triangle with 2 sides of equal length.
  • Scalene: It's a triangle with 3 sides of differing lengths.
  • Not A Triangle: The given values of A, B, and C don't form a triangle.

Return _the result table in any order_.

The result format is in the following example.

 

Example 1:

Input: 
Triangles table:
+----+----+----+
| A  | B  | C  |
+----+----+----+
| 20 | 20 | 23 |
| 20 | 20 | 20 |
| 20 | 21 | 22 |
| 13 | 14 | 30 |
+----+----+----+
Output: 
+----------------+
| triangle_type  | 
+----------------+
| Isosceles    | 
| Equilateral  |
| Scalene    |
| Not A Triangle |
+----------------+
Explanation: 
- Values in the first row from an Isosceles triangle, because A = B.
- Values in the second row from an Equilateral triangle, because A = B = C.
- Values in the third row from an Scalene triangle, because A != B != C.
- Values in the fourth row cannot form a triangle, because the combined value of sides A and B is not larger than that of side C.

解法

方法一:使用 CASE WHEN 语句

我们可以使用 CASE WHEN 语句来判断三角形的类型。

首先,我们需要判断三个边是否能够构成一个三角形。如果不能,我们返回 Not A Triangle

然后,我们判断三个边的长度是否相等。如果相等,我们返回 Equilateral

接着,我们判断是否有两个边的长度相等。如果有,我们返回 Isosceles

否则,说明三个边的长度都不相等,我们返回 Scalene

# Write your MySQL query statement below
SELECT
  CASE
    WHEN A + B <= C
    OR A + C <= B
    OR B + C <= A THEN 'Not A Triangle'
    WHEN A = B
    AND B = c THEN 'Equilateral'
    WHEN (A = B) + (B = C) + (A = C) = 1 THEN 'Isosceles'
    ELSE 'Scalene'
  END AS triangle_type
FROM Triangles;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文