본문 바로가기
서버/MYSQL

[MYSQL] Hackerrank - Type of Triangle

by HDobby 2022. 12. 21.

https://www.hackerrank.com/challenges/what-type-of-triangle/problem

 

Type of Triangle | HackerRank

Query a triangle's type based on its side lengths.

www.hackerrank.com

문제

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:

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

대강 해석

TRIANGLES 테이블에 있는 3변의 상태를 아래의 조건에 맞게 출력해 주세요.

Equilateral : 3변의 길이가 같은 정삼각형

Isosceles : 2변의 길이가 같은 이등변삼각형

Scalene : 3변의 길이가 모두 다른 삼각형

Not A Triangle : 삼각형이 아닌 경우

 

풀이

  • case when 혹은 if else문을 사용 하여 정삼각형, 삼각형이 아닌 경우, 이등변 혹은 일반 삼각형인지를 분류한다.
  • 삼각형이 아닌 경우를 먼저 분류하지 않는다면 삼각형이 아니어도 이등변 혹은 일반 삼각형으로 분류 될 수 있으므로 틀리게 된다.

 

코드

더보기
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 and b != c and c != a then "Scalene"
        ELSE "Isosceles"
    END
from TRIANGLES;
728x90

'서버 > MYSQL' 카테고리의 다른 글

[MYSQL] Hackerrank - Occupations  (0) 2022.12.31
[MYSQL] Hackerrank - The PADS  (0) 2022.12.22
[MYSQL] Hackerrank - Employee Salaries  (0) 2022.12.16
[MYSQL] Hackerrank - Employee Names  (0) 2022.12.16
[MYSQL] Hackerrank - Higher Than 75 Marks  (0) 2022.12.14

댓글