📌Back-End/프로그래머스

Java 프로그래머스 코딩 기초 트레이닝 Day 25 이차원 리스트(배열)

구 일 2024. 6. 21. 16:59
728x90
반응형

 

정수를 나선형으로 배치하기

프로그래머스 정수를 나선형으로 배치하기 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 25
 * 정수를 나선형으로 배치하기
 */
public class Main1 {
    public static void main(String[] args) {
        int[][] result = solution(4);

        for (int[] arr : result) {
            for (int num : arr) {
                System.out.printf("%2d ", num);
            }
            System.out.println();
        }
        System.out.println();

        result = solution(5);

        for (int[] arr : result) {
            for (int num : arr) {
                System.out.printf("%2d ", num);
            }
            System.out.println();
        }
        System.out.println();
    }

    public static int[][] solution(int n) {
        int[][] answer = new int[n][n];

        int num = 1;
        int rowStart = 0;
        int rowEnd = n - 1;
        int colStart = 0;
        int colEnd = n - 1;

        while (num <= n * n) {
            for (int i = colStart; i <= colEnd; i++) {
                answer[rowStart][i] = num++;
            }
            rowStart++;

            for (int i = rowStart; i <= rowEnd; i++) {
                answer[i][colEnd] = num++;
            }
            colEnd--;

            for (int i = colEnd; i >= colStart; i--) {
                answer[rowEnd][i] = num++;
            }
            rowEnd--;

            for (int i = rowEnd; i >= rowStart; i--) {
                answer[i][colStart] = num++;
            }
            colStart++;
        }

        return answer;
    }
}

 

 

특별한 이차원 배열 2

프로그래머스 특별한 이차원 배열 2 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 25
 * 특별한 이차원 배열 2
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(
                solution(new int[][]{
                        {5, 192, 33},
                        {192, 72, 95},
                        {33, 95, 999}}
                )
        );

        System.out.println(
                solution(new int[][]{
                        {19, 498, 258, 587},
                        {63, 93, 7, 754},
                        {258, 7, 1000, 723},
                        {587, 754, 723, 81}}
                )
        );
    }

    public static int solution(int[][] arr) {
        int answer = 0;

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] == arr[j][i]) {
                    answer = 1;
                } else if (arr[i][j] != arr[j][i]) {
                    answer = 0;
                    break;
                }
            }

            if (answer == 0) {
                break;
            }
        }

        return answer;
    }
}

 

 

정사각형으로 만들기

프로그래머스 정사각형으로 만들기 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 25
 * 정사각형으로 만들기
 */
public class Main3 {
    public static void main(String[] args) {
        int[][] result = solution(new int[][]{{572, 22, 37}, {287, 726, 384}, {85, 137, 292}, {487, 13, 876}});
        for (int[] arr : result) {
            for (int num : arr) {
                System.out.printf("%4d ", num);
            }
            System.out.println();
        }
        System.out.println();

        result = solution(new int[][]{{57, 192, 534, 2}, {9, 345, 192, 999}});
        for (int[] arr : result) {
            for (int num : arr) {
                System.out.printf("%4d ", num);
            }
            System.out.println();
        }
        System.out.println();

        result = solution(new int[][]{{1, 2}, {3, 4}});
        for (int[] arr : result) {
            for (int num : arr) {
                System.out.printf("%4d ", num);
            }
            System.out.println();
        }
        System.out.println();
    }

    public static int[][] solution(int[][] arr) {
        int x = arr.length;
        int y = arr[0].length;
        int n = Math.max(x, y);

        int[][] answer = new int[n][n];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                answer[i][j] = arr[i][j];
            }
        }

        return answer;
    }
}

 

 

이차원 배열 대각선 순회하기

프로그래머스 이차원 배열 대각선 순회하기 자바

 

 

/**
 * 코딩 기초 트레이닝 Day 25
 * 이차원 배열 대각선 순회하기
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(new int[][]{{0, 1, 2},{1, 2, 3},{2, 3, 4},{3, 4, 5}}, 2));
    }

    public static int solution(int[][] board, int k) {
        int answer = 0;

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (i + j <= k) {
                    answer += board[i][j];
                }
            }
        }

        return answer;
    }
}

 

 

728x90
반응형