Skip to content

Latest commit

 

History

History
73 lines (43 loc) · 1.42 KB

610. Triangle-Judgement.md

File metadata and controls

73 lines (43 loc) · 1.42 KB

题目

一个小学生 Tim 的作业是判断三条线段是否能形成一个三角形。

然而,这个作业非常繁重,因为有几百组线段需要判断。

假设表 triangle 保存了所有三条线段的三元组 x, y, z ,你能帮 Tim 写一个查询语句,来判断每个三元组是否可以组成一个三角形吗?

x y z
13 15 30
10 20 15

对于如上样例数据,你的查询语句应该返回如下结果:

x y z triangle
13 15 30 No
10 20 15 Yes

建表语句:

Create table If Not Exists triangle (x int, y int, z int)
Truncate table triangle
insert into triangle (x, y, z) values ('13', '15', '30')
insert into triangle (x, y, z) values ('10', '20', '15')

思路

三角形任意两边之和大于第三边,任意两边之差小于第三边。

case...when... 子句中实现上述条件。

例如:

SELECT OrderID, Quantity,
CASE
    WHEN Quantity > 30 THEN "The quantity is greater than 30"
    WHEN Quantity = 30 THEN "The quantity is 30"
    ELSE "The quantity is under 30"
END
FROM OrderDetails;

代码

select x,y,z, 

case
    when x+y>z and y+z>x and x+z>y then 'Yes'
    else 'No'
end
as triangle

from triangle