Milky's note

[HackerRank](E) Type of Triangle 본문

SQL/SQL 코딩 테스트-HackerRank

[HackerRank](E) Type of Triangle

밀뿌 2022. 3. 18. 02:02

https://www.hackerrank.com/challenges/what-type-of-triangle/problem?isFullScreen=true 

 

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.

[답]

-mysql

SELECT 
CASE 
WHEN A=B AND A=C AND B=C THEN 'Equilateral' 
WHEN A>=B+C OR B>=A+C OR C>=A+B THEN 'Not A Triangle'
WHEN A=B OR A=C OR B=C THEN 'Isosceles' 
ELSE 'Scalene' END
FROM TRIANGLES;

 

이 문제는 case when 문으로 해결하면 된다. 

case 문의 문법은

case

when 조건 then '출력'

else '출력' end

이므로, 삼각형의 해당되는 조건을 입력해준 뒤, 맞는 삼각형을 출력한다.

'SQL > SQL 코딩 테스트-HackerRank' 카테고리의 다른 글

[HackerRank](M) Binary Tree Nodes  (0) 2022.03.18
[HackerRank](M) The PADS  (0) 2022.03.18
[HackerRank](E) Employee Salaries  (0) 2022.03.18
[HackerRank](E) Employee Names  (0) 2022.03.18
[HackerRank](E) Higher Than 75 Marks  (0) 2022.03.18
Comments