ror_coding

[HackerRank] Type of Triangle 본문

Coding Test/SQL

[HackerRank] Type of Triangle

ro_rdil_31 2025. 6. 14. 14:46
728x90

삼각형이 되기 위한 세 변의 조건.

 

Question

 

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
 

 

Point

 

  • 세 변의 길이가 같을 때 먼저,
  • 한 변이 다른 두 변의 합보다 크거나 같을 때 삼각형이 아니기 때문에 예외 처리를 두 번째에,
  • 이후 두 변이 같을 때 (이때, 세 변이 같은 케이스는 첫 번째때 리턴했기 때문에 문제 없음)
  • 마지막 else 구문은 세 변의 길이가 모두 다를 때로 처리됨.

 

Code

 

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 A=C OR B=C THEN 'Isosceles'
        ELSE 'Scalene'  
    END
FROM TRIANGLES

 

now me

728x90

'Coding Test > SQL' 카테고리의 다른 글

[HackerRank] Placements  (1) 2025.06.14
[HackerRank] The Report  (0) 2025.06.14
[HackerRank] Symmetric Pairs  (0) 2025.06.14
[HackerRank] Binary Tree Nodes  (0) 2025.04.05
[HackerRank] Occupations  (0) 2025.04.05