본문 바로가기

프로그래머스/lv0

삼각형의 완성조건 (1)

/* 삼각형의 완성조건 (1)
 * 선분 세 개로 삼각형을 만들기 위해서는 다음과 같은 조건을 만족해야 합니다.
 * 가장 긴 변의 길이는 다른 두 변의 길이의 합보다 작아야 합니다.
 * 삼각형의 세 변의 길이가 담긴 배열 sides이 매개변수로 주어집니다.
 * 세 변으로 삼각형을 만들 수 있다면 1, 만들 수 없다면 2를 return
 *
 * sides            result
 * [1, 2, 3]        2       가장 큰 변인 3이 나머지 두 변의 합 3과 같으므로 삼각형 X. 2 return
 * [3, 6, 2]        2       가장 큰 변인 6이 나머지 두 변의 합 5보다 크므로 삼각형 X. 2 return
 * [199, 72, 222]   1       가장 큰 변인 222가 나머지 두 변의 합 271보다 작으므로
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class programmer_0_28 {
    static int[] ar1 = {1,2,3};
    static int[] ar2 = {3,6,2};
    static int[] ar3 = {199,72,222};
 
    public int solution(int[] sides) {
        int answer = 0;
        int max = 0;
        for(int i : sides){
            if(i > max) max = i;
            answer += i;
        }
        
        if(max < (answer - max))    return 1;
        else                        return 2;
    }
    public static void main(String args[]){
        programmer_0_28 t = new programmer_0_28();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(ar1));
        System.out.println("---------------------------------------");
        // System.out.println("result2 = " + t.solution(ar2));
        // System.out.println("---------------------------------------");
        // System.out.println("result3 = " + t.solution(ar3));
        // System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

369게임  (0) 2022.12.05
가까운 수  (1) 2022.12.05
분수의 덧셈  (1) 2022.12.05
중복된 문자 제거  (0) 2022.12.05
k의 개수  (0) 2022.12.05