본문 바로가기

프로그래머스/lv0

직사각형의 넓이

/* 직사각형의 넓이
 * 2차원 좌표 평면에 변이 축과 평행한 직사각형이 있습니다.
 * 직사각형 네 꼭짓점의 좌표 [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]가 담겨있는
 * 배열 dots가 매개변수로 주어질 때,
 * 직사각형의 넓이를 return
 *
 * dots                                 result
 * [[1, 1], [2, 1], [2, 2], [1, 2]]     1       가로, 세로 길이는 각각 1, 1
 * [[-1, -1], [1, 1], [1, -1], [-1, 1]] 4       직사각형의 가로, 세로 길이는 각각 2, 2
 */
 
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
import java.lang.*;
public class programmer_0_88 {
    static int[][] a1 = {{11}, {21}, {22}, {12}};
    static int[][] a2 = {{46}, {03}, {40}, {83}};
    public int solution(int[][] dots) {
        int answer = 0;
        int x = dots[0][0]; int length_x = 0;
        int y = dots[0][1]; int length_y = 0;
        for(int i = 1; i < dots.length; i++){
            if(x != dots[i][0]) length_x = x - dots[i][0];
            if(y != dots[i][1]) length_y = y - dots[i][1];
        }
        System.out.println(length_x + " / " + length_y);
 
        return Math.abs(length_x * length_y);   //절대값으로 표현
    }
    public static void main(String args[]){
        programmer_0_88 t = new programmer_0_88();
        // System.out.println("---------------------------------------");
        // System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
    }
}
cs

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

특이한 정렬  (2) 2022.12.07
유한소수 판별하기  (0) 2022.12.07
컨트롤 제트  (0) 2022.12.07
삼각형의 완성조건 (2)  (1) 2022.12.07
옹알이(1)  (0) 2022.12.06