列表

详情


610. 判断三角形

表: Triangle

+-------------+------+
| Column Name | Type |
+-------------+------+
| x           | int  |
| y           | int  |
| z           | int  |
+-------------+------+
(x, y, z)是该表的主键列。
该表的每一行包含三个线段的长度。

 

写一个SQL查询,每三个线段报告它们是否可以形成一个三角形。

以 任意顺序 返回结果表。

查询结果格式如下所示。

 

示例 1:

输入: 
Triangle 表:
+----+----+----+
| x  | y  | z  |
+----+----+----+
| 13 | 15 | 30 |
| 10 | 20 | 15 |
+----+----+----+
输出: 
+----+----+----+----------+
| x  | y  | z  | triangle |
+----+----+----+----------+
| 13 | 15 | 30 | No       |
| 10 | 20 | 15 | Yes      |
+----+----+----+----------+

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
# Write your MySQL query statement below

pythondata 解法, 执行用时: 391 ms, 内存消耗: 65.5 MB, 提交时间: 2024-05-27 11:25:21

import pandas as pd

def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame:
    triangle['triangle'] = triangle.apply(lambda x: 'Yes' if max(x) < sum(x) - max(x) else 'No', axis=1)
    return triangle

def triangle_judgement2(triangle: pd.DataFrame) -> pd.DataFrame:
    # 使用apply方法和lambda函数对每行进行条件判断
    triangle['triangle'] = triangle.apply(
        lambda row: 'Yes' if row['x'] + row['y'] > row['z'] and row['x'] + row['z'] > row['y'] and row['y'] + row['z'] > row['x'] else 'No',
        axis=1
    )
    return triangle

mysql 解法, 执行用时: 168 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:54:21

# Write your MySQL query statement below
SELECT x, y, z
	, if(x + y <= z
	OR x + z <= y
	OR y + z <= x, "No", "Yes") AS triangle
FROM triangle;

mysql 解法, 执行用时: 167 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:54:00

# Write your MySQL query statement below
SELECT
    x,y,z,
    IF(ABS((power(x,2)-power(y,2)-power(z,2))/(2*y*z)) < 1,'Yes','No') AS 'triangle'
FROM 
    triangle;

mysql 解法, 执行用时: 265 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 11:53:33

# Write your MySQL query statement below
SELECT 
    x,
    y,
    z,
    CASE
        WHEN x + y > z AND x + z > y AND y + z > x THEN 'Yes'
        ELSE 'No'
    END AS 'triangle'
FROM
    triangle
;

上一题