매일 Algorithm

신재원·2023년 5월 4일
0

Algorithm

목록 보기
113/243

코드업 (1096번)

import java.util.Scanner;

public class problem370 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        int[][] graph = new int[19][19];


        for(int i = 0; i < size; i++){
            int x = in.nextInt();
            int y = in.nextInt();
            graph[x - 1][y - 1] = 1;
        }

        for(int i = 0; i < 19; i++){
            for(int j = 0; j < 19; j++){
                System.out.print(graph[i][j] + " ");
            }
            System.out.println();
        }
    }
}

프로그래머스 (대소문자 바꿔서 출력하기)

import java.util.Scanner;

public class problem371 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.next();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < a.length(); i++){
            char ch = a.charAt(i);

            if(ch >= 'a' && ch <= 'z'){
                sb.append(Character.toUpperCase(ch));
            }
            else{
                sb.append(Character.toLowerCase(ch));
            }
        }
        System.out.println(sb.toString());
    }
}

코드업 (4596번)

import java.util.Scanner;

public class CodeUp4596 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int max = 0, row = 0, col = 0;


        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                int number = in.nextInt();

                if (number > max) {
                    max = number;
                    row = i + 1; // 열
                    col = j + 1; // 행
                }
            }
        }

        System.out.println(max);
        System.out.print(row + " " + col);

    }
}

코드업 (1097번)

import java.util.Scanner;

public class CodeUp1097 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int[][] graph = new int[19][19];
        for (int i = 0; i < 19; i++) {
            for (int j = 0; j < 19; j++) {
                graph[i][j] = in.nextInt();
            }
        }

        // 뒤집는 횟수
        int change = in.nextInt();

        // 10 10
        // 12 12
        for (int i = 0; i < change; i++) {
            // 0번 인덱스부터 시작함으로 -1
            int x = in.nextInt() - 1;
            int y = in.nextInt() - 1;

            // 0이면 1로, 1이면 0으로 바꿔준다. 
            // 행과 열이 겹치는 부분이 1로 반환된다.
            for (int j = 0; j < graph.length; j++) {
                if (graph[x][j] == 0) {
                    graph[x][j] = 1;
                } else {
                    graph[x][j] = 0;
                }
            }

            for (int j = 0; j < graph.length; j++) {
                if (graph[j][y] == 0) {
                    graph[j][y] = 1;
                } else {
                    graph[j][y] = 0;
                }
            }
        }


        for (int i = 0; i < 19; i++) {
            for (int j = 0; j < 19; j++) {
                System.out.print(graph[i][j] + " ");
            }
            System.out.println();
        }


    }
}

코드업 (1098번)

import java.util.Scanner;

public class CodeUp1098 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int xSize = in.nextInt();
        int ySize = in.nextInt();
        int[][] board = new int[xSize][ySize];
        int stick = in.nextInt();

        for (int i = 0; i < stick; i++) {
            int length = in.nextInt(); // 길이
            int dir = in.nextInt(); // 방향
            int xComma = in.nextInt(); // x좌표
            int yComma = in.nextInt(); // y좌표

            for (int j = 0; j < length; j++) {
                // 방향이 0일경우 길이 만큼 가로로 찍는다.
                if (dir == 0) {
                    board[xComma - 1][yComma - 1 + j] = 1;
                }
                // 0이 아닐경우는 길이 만큼 세로로 찍는다.
                else {
                    board[xComma - 1 + j][yComma - 1] = 1;
                }
            }

        }

        for (int i = 0; i < xSize; i++) {
            for (int j = 0; j < ySize; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }
}

프로그래머스 (Level 1)

public class problem372 {
    class Solution {
        public int[] solution(String[] name, int[] yearning, 
                              String[][] photo) {
            int[] answer = new int[photo.length];

            // 2차원 배열에서 length를 하게되면 열의 길의가 반환됩니다.
            for(int i = 0; i < photo.length; i++){

                // photo[i].length로 조건을 걸어줘야 
                // 해당 열의 행의 길이만큼 반복합니다.
                for(int j = 0; j < photo[i].length; j++){

                    for(int k = 0; k < name.length; k++){
                        // 이름이 동일하면, 해당 이름의 추억점수를 누적해줍니다.
                        if(name[k].equals(photo[i][j])){
                            answer[i] += yearning[k];
                        }
                    }
                }
            }
            return answer;
        }
    }
}

0개의 댓글