매일 Algorithm

신재원·2023년 3월 25일
0

Algorithm

목록 보기
76/243

백준 1236번 (Bronze 1)

import java.util.Scanner;

public class problem237 {

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

        int n = in.nextInt();
        int m = in.nextInt();
        in.nextLine();
        char[][] arr = new char[n][m];

        for (int i = 0; i < n; i++) {
            String str = in.next();
            for (int j = 0; j < m; j++) {
                arr[i][j] = str.charAt(j);
            }
        }
        // 모든 행과 열에는 경비원이 한명씩 존재
        int countA = 0;
        int countB = 0;

        // 행 경비원 검증
        for (int i = 0; i < n; i++) {
            boolean flag = true;
            for (int j = 0; j < m; j++) {
                if (arr[i][j] == 'X') {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                countA++;
            }
        }

        // 열 경비원 검증
        for (int i = 0; i < m; i++) {
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if (arr[j][i] == 'X') {
                    flag = false;
                    break;
                }

            }
            if (flag) {
                countB++;
            }
        }
        int result = Math.max(countA, countB);
        System.out.println(result);

    }
}

백준 2204번 (Bronze 1)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class problem238 {

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


        while (true) {
            int size = in.nextInt();
            // 0을 입력받으면 종료
            if (size == 0) {
                break;
            }
            List<String> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                list.add(in.next());
            }

            // 대소문자 구분 없이 정렬 :  String.CASE_INSENSITIVE_ORDER
            Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
            System.out.println(list.get(0));
        }
    }
}

백준 1813번 (Bronze 1)

import java.util.Scanner;

public class problem239 {

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


        int size = in.nextInt();
        int[] arr = new int[51];

        for (int i = 0; i < size; i++) {
            int index = in.nextInt();
            arr[index]++;
        }

        for (int j = size; j >= 0; j--) {
            // 순회하면 숫자의 값과 갯수를 검증
            if (arr[j] == j) {
                System.out.print(j);
                return;
            }
        }
        System.out.print(-1);


    }
}

백준 2435번 (Bronze 1)

import java.util.Scanner;

public class problem240 {

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


        int size = in.nextInt();
        int k = in.nextInt();
        int[] arr = new int[size];
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < size; i++) {
            arr[i] = in.nextInt();

        }
        for (int j = 0; j <= size - k; j++) {
            int sum = 0; // 초기화
            for (int p = 0; p < k; p++) {
                sum += arr[j + p];
            }
            // 온도의 합 최대 값 구하기
            if (sum > max) {
                max = sum;
            }
        }


        System.out.println(max);
    }
}

0개의 댓글