[hackerrank] Type of Triangle

yenpkr·2025년 4월 6일
0

sql

목록 보기
80/91

문제

세 변의 길이를 이용하여 삼각형의 타입을 나타내시오.

Equilateral: 세 변 모두 같음
Isosceles: 두 변 같음
Scalene: 세 변 모두 다름
Not A Triangle: 삼각형이 아님

  • 삼각형의 조건 중 하나
    한 변이 남은 두 변의 길이의 합보다 작아서는 안된다.
    작을 경우 → Not A triangle

제출

select (case when a=b and b=c and c=a then 'Equilateral'
        when a+b<=c or b+c<=a or c+a<=b then 'Not A Triangle'
        when (a=b and b!=c) or (b=c and a!=b) or (c=a and b!=c) then 'Isosceles'
        when a!=b and b!=c and c!=a then 'Scalene' end) as 'triangle'
from triangles

💁‍♀️ 더 간단하게 !

select (case when a=b and b=c then 'Equilateral'
        when a+b<=c or b+c<=a or c+a<=b then 'Not A Triangle'
        when a=b or b=c or c=a then 'Isosceles'
        else 'Scalene' end) as triangle
from triangles

case when 구문이 if ~ else if ~ else 구문과 같다는걸 항상 까먹는다.

세 변이 같은 경우 → 삼각형 아닌 경우 → 두 변만 같을 경우 (세 변이 모두 같을 경우는 앞에서 필터링 되었으니 when a=b or b=c or c=a then 'Isosceles' 이렇게 작성해도 무방) → 나머지는 세 변 모두 다를 경우

0개의 댓글