Computer Science (CS)/알고리즘

[백준/Java] 2798번 블랙잭

eune7 2023. 6. 1. 14:45
728x90
반응형

 

 

 

 

 

 

문제

 

 

 

정답 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int[] nums = new int[N];
        int result = 0;

        st = new StringTokenizer(br.readLine());
        for(int i = 0; i<N; i++){
            nums[i] = Integer.parseInt(st.nextToken());
        }

        for(int i = 0; i < N; i++){
            if(nums[i] > M) continue;
            for(int j = 0; j < N; j++){
                if(nums[i] + nums[i] > M || nums[i] == nums[j]) continue;
                for(int k = 0; k < N; k++){
                    if(nums[i] == nums[k] || nums[j] == nums[k]) continue;
                    int sum = nums[i] + nums[j] + nums[k];
                    if(sum == M) {
                        result = sum;
                        break;
                    }
                    else if(result < sum && sum < M)
                        result = sum;
                }
            }
        }
        System.out.println(result);

        br.close();
    }
}
  • 메모리 14468KB
  • 시간 156ms

브루트 포스 카테고리에 들어왔습니다.

먼저 브루트 포스란, 무식하다는 뜻을 가지고 있어요.

즉, 경우의 수를 다 따져보는 알고리즘입니다.

 

그래서 값을 그냥 다 들어가봐서 M이 되는지 혹은 M에 더 가까운 수가 되었는지 체크해주면 됩니다.

 

 

 

 

 

728x90
반응형