728x90
반응형

Summary

이 문제는 삼각형의 조건에 충족하는지 그리고 조건에 충족한다면 어떤 삼각형인지 타입을 구분하는 문제라고 보면 된다.
삼각형이 성립되는 조건에 대해서 알고 있어야 하고 조건 하에 각 삼각형의 타입을 어떻게 구분할 수 있을지 생각해봐야 한다.

삼각형이 성립되는 조건은 다음과 같다.

 

(가장 큰 변의 길이) < (나머지 두 변의 합)

 

예를 들어, A B C 로 이루어진 삼각형에서 A 변의 길이가 가장 크다면 A < (B + C) 가 되어야 삼각형이 성립된다.

 

이 문제에서 정의하는 삼각형의 타입은 다음과 같다.

세 변의 길이가 모두 같으면 '정삼각형'
두 변의 길이가 같고 나머지 하나의 길이가 다르면  '이등변삼각형'
세 변의 길이가 다 다르면 '일반적인 삼각형'
그리고 삼각형이 성립하지 않는다면 '삼각형이 아니다'

 

문제의 내용은 다음과 같다.

 

Problem

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

Explanation

Values in the tuple  form an Isosceles triangle, because . 
Values in the tuple  form an Equilateral triangle, because . Values in the tuple  form a Scalene triangle, because . 
Values in the tuple  cannot form a triangle because the combined value of sides  and  is not larger than that of side .

 

Submit Code

내가 제출한 코드는 다음과 같다.

SELECT
    CASE
        WHEN (a + b > c) AND (a + c > b) AND (b + c > a) THEN
            CASE
                WHEN a = b AND a = c AND b = c THEN "Equilateral"
                WHEN a = b OR a = c OR b = c THEN "Isosceles"
                ELSE "Scalene"
            END
        ELSE "Not A Triangle"
    END
FROM TRIANGLES

 

Mysql 로 작성했고 CASE 구문을 사용해 조건을 작성해 문제를 풀 수 있었다.
CASE 구문에서는 삼각형이 성립하는 조건을 주어 조건에 만족하면 어떤 삼각형인지 구분하도록 코드를 작성했다.
CASE 구문과 삼각형의 조건 등을 잘 이해하고 있다면 쉽게 풀 수 있는 내용이었다.

728x90
반응형
복사했습니다!