SQL/코딩테스트 연습

[HackerRank] Type of Triangle

천꾸냥 2026. 8. 19. 23:50
320x100
 

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.

Input Format

The TRIANGLES table is described as follows:

Each row in the table denotes the lengths of each of a triangle's three sides.

Sample Input

Sample Output

Isosceles
Equilateral
Scalene
Not A Triangle

 

📝 코드 

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 OR B = C OR A = C THEN 'Isosceles'
        ELSE 'Scalene'
    END AS TRIANGLE_TYPE
FROM TRIANGLES;
  • CASE WHEN 구문 사용
  • 첫 번째 조건 : 삼각형이 아예 안되는 경우
  • 두 번째 조건 : 정삼각형이 되는 경우
  • 세 번째 조건 : 이등변 삼각형이 되는 경우
    • 앞서 정삼각형을 미리 걸렀기 때문에 두 변만 같은 경우들만 남아서 판별하게 됨
  • 그 외 조건이라면 세 변이 모두 다른 경우
728x90